2009-09-24 23 views
32

UITableViewを連絡先エディタのテーブルのように動作させたい、つまりユーザーが編集を押して、新しいセクションを追加する行を各セクションの下部に表示する必要があります。UITableViewでの挿入行の使用

私はこれを行うには以下のコードを使用していますが、連絡先にあるように円滑な移行がないという問題があります。代わりに、新しい行が突然表示されます。アニメーションを取得するにはどうしたらいいですか?

また、「新しいカテゴリを追加する」行のクリックにどのように反応しますか?現在の実装では行はクリックできません。

ユーザーが編集を開始するときにデータを再読み込みする必要がありますか?私はそうしないと挿入行が決して描画されないので、これをやっています。

ありがとうございました。行上のクリックに応答

- (void)setEditing:(BOOL)editing animated:(BOOL)animated { 
    [super setEditing:editing animated:animated]; 
    [self.tableView setEditing:editing animated:animated]; 
    [tableView reloadData]; 
} 

- (NSInteger)tableView:(UITableView *)_tableView numberOfRowsInSection:(NSInteger)section { 
    // ... 
    if(self.tableView.editing) 
     return 1 + rowCount; 
} 

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // ..... 
    NSArray* items = ...; 
    if(indexPath.row >= [items count]) { 
     cell.textLabel.text = @"add new category"; 
    } 
    // ... 

    return cell; 
} 

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSArray* items = ...; 

    if(indexPath.row == [items count]) 
     return UITableViewCellEditingStyleInsert; 

    return UITableViewCellEditingStyleDelete; 
} 
+2

これは(以下、答えと一緒に)非常に有用だった:

// Assuming swipeMode is a BOOL property in the class extension. - (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath { // Invoked only when swiping, not when pressing the Edit button. self.swipeMode = YES; } - (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath { self.swipeMode = NO; } 

あなたのコードは、小さな変更を必要とします。 'tableView:editingStyleForRowAtIndexPath:'は '=='を使いますが、 'tableView:cellForRowAtIndexPath:'の行数との比較は '> ='を使います。大きな問題ではありませんが、それらの間で一貫している必要があります。 '> ='はInsert行の偶発的な二重追加をカバーし、 '=='はその状況につながるコードエラーの例外を投げることによって助けになります。 –

答えて

36

私は1つ欠けていました。代わりにreloadDataを呼び出すsetEditingに:,私が行っているべきである:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated { 
    [super setEditing:editing animated:animated]; 
    [self.tableView setEditing:editing animated:animated]; // not needed if super is a UITableViewController 

    NSMutableArray* paths = [[NSMutableArray alloc] init]; 

    // fill paths of insertion rows here 

    if(editing) 
     [self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationBottom]; 
    else 
     [self.tableView deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationBottom]; 

    [paths release]; 
} 
+3

ビルのおかげで、これは私にとって非常に便利でした。私は似たようなコードで数時間悩まされましたが、それはかなり正しいものでした。これは計り知れないほどの助けとなりました。 – hkatz

+0

うれしかったよ! – Bill

+0

commitEditingStyle:forRowAtIndexPathはどうですか? Appleのドキュメントでは、テーブルビューが最初に編集モードに入るときにsetEditingが呼び出されます...そのポイントで挿入/削除する行をどのように知っていますか? – shim

5

indexPath.row == [items count]ため、didSelectRowAtIndexPath方法で行われるかもしれません。アニメーションについては、hereinsertRowsAtIndexPaths:withRowAnimation:メソッドでご覧ください。それを使用する方法についての投稿がありますhere

0

強調表示された溶液の望ましくない副作用は、ユーザがただ一つの行をスワイプするとき、行も挿入されている「追加」することである(スワイプことを条件とします有効になっています)。次のコードは、このジレンマを解決します

- (void)setEditing:(BOOL)editing animated:(BOOL)animated { 
    [super setEditing:editing animated:animated]; 
    [self.tableView setEditing:editing animated:animated]; // not needed if super is a UITableViewController 

    if (!self.swipeMode) { 

     NSMutableArray* paths = [[NSMutableArray alloc] init]; 

     // fill paths of insertion rows here 

     if(editing) 
      [self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationBottom]; 
     else 
      [self.tableView deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationBottom]; 
     [paths release]; 
    } 
} 
関連する問題