2011-09-10 10 views
0

したがって、私はそれに応じて対応できるように、挿入操作と削除操作を区別する方法を決めたいと思っています。現在、私は「完了」、「編集」を作成し、私はボタンの機能を「コールバック」は、いわゆるとしてこれらの3を持っている編集操作が削除かUITableViewControllerで追加するかを確認する

- (void)initializeNavigationBarButtons 
{ 
    UIBarButtonItem *newEditButton = 
    [[UIBarButtonItem alloc] 
    initWithBarButtonSystemItem:UIBarButtonSystemItemEdit 
    target:self action:@selector(performEdit:)]; 

    self.editButton = newEditButton; 
    [newEditButton release]; 

    self.navigationItem.rightBarButtonItem = self.editButton; 

    UIBarButtonItem *newDoneButton = 
    [[UIBarButtonItem alloc] 
    initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
    target:self action:@selector(performDone:)]; 

    self.doneButton = newDoneButton; 
    [newDoneButton release]; 

    UIBarButtonItem *newAddButton = 
    [[UIBarButtonItem alloc] 
    initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
    target:self action:@selector(performAdd:)]; 

    self.addButton = newAddButton; 
    [newAddButton release]; 
    self.navigationItem.leftBarButtonItem = self.addButton; 

} 

ボタンを「追加」するには、このコードを持っている:

- (void)performDone:(id)paramSender 
{ 
    [self.tableView setEditing:NO animated:YES]; 


    [self.navigationItem setRightBarButtonItem:self.editButton 
             animated:YES]; 

    [self.navigationItem setLeftBarButtonItem:self.addButton 
             animated:YES]; 
} 

- (void)performEdit:(id)paramSender 
{ 
    NSLog(@"Callback Called"); 
    [self.tableView setEditing:YES animated:YES]; 

    [self.navigationItem setRightBarButtonItem:self.doneButton 
             animated:YES]; 

    [self.navigationItem setLeftBarButtonItem:self.doneButton 
            animated:YES]; 
} 

- (void)performAdd:(id)paramSender 
{ 
    [self.tableView setEditing:YES animated:YES]; 

    [self.navigationItem setRightBarButtonItem:self.doneButton 
             animated:YES]; 

    [self.navigationItem setLeftBarButtonItem:self.doneButton 
             animated:YES]; 
} 

と、ここで私はそれがアドオンであるかどうかを決定するか、または操作を削除「することになって」AMところです:私はself.isDeletingとself.isAddingを設定することが出来るのですどこ

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView 
      editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *output = (isDeleting) ? @"Deleting" : @"Adding"; 

    NSLog(@"%@", output); 

    UITableViewCellEditingStyle result = UITableViewCellEditingStyleNone; 

    if ([tableView isEqual:self.tableView]){ 
     if (self.isDeleting == YES){ 
      result = UITableViewCellEditingStyleDelete; 
     } 
     else{ 
      result = UITableViewCellEditingStyleInsert; 
     } 
    } 

    return result; 
} 

しかし、私は知りません。コールバックでそれらを設定しようとしましたが、tableView:cellEditingStyleForRowAtIndexPath:が最初に呼び出され、私のviewDidLoadのデフォルト値はNOです。

したがって、tableView:cellEditingStyleForRowAtIndexPath:メソッドで適切に動作するように、isAddingおよびisDeletingの値を適切に設定するにはどうすればよいですか?

ありがとうございます!

答えて

1

ナビゲーションバーに「追加」ボタンがある場合は、テーブルビューを編集可能にするのではなく、「追加」アクションを実行してみてください。セルのコンテンツがあるときに、セルの編集スタイルをUITableViewCellEditingStyleInsertに設定することにはあまり意味がありません。通常の流れは、追加アクションを実行するテーブルの外に(たとえば:ナビゲーションバーの)「追加」ボタンを持つか、または編集スタイル中にテーブルビュー内の最後(または最初の)のセルとして、セルが押されると、追加アクションが実行されます。

ナビゲーションバーの[追加]ボタンをクリックして、すべてのセルをUITableViewCellEditingStyleDeleteで編集可能にするか、[編集]/[完了]ボタンだけを保持し、編集中に新しいセルを追加します編集可能)はUITableViewCellEditingStyleInsertで編集可能です。

サイドノート:if ([tableView isEqual:self.tableView])の代わりに、同じインスタンスであるかどうかをチェックしたい場合は、if (tableView == self.tableView)を使用してください。

関連する問題