2009-07-22 12 views
5

私はUITableViewを持っています。ユーザーが行をずらすかフリックするときに行を削除する機能をユーザに提供したいと思います。私は円形の赤いボタンに-ve記号を付ける編集スタイルを知っています。しかし、フリックスタイルを実装する方法。私はそれを使用して多くのアプリケーションを見たので、リンゴはそれのための任意の組み込みの代理人を提供するか、または我々はそれのための独自のコントローラを記述する必要があります。iPhoneでテーブルの行を削除する方法

+0

あなたはすでに知っている方法を教えていただけますか?私はそれを実装したいと思う。 – rptwsthi

答えて

32

は、あなたがテーブルビューのデリゲート

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 

メソッドを実装する必要が影響し、これは削除のためにスワイプ相互作用へのアクセスを提供します。私は通常、スワイプのやりとりがユーザから少し隠れてしまう傾向があるため、削除が可能なテーブルビューの編集インタラクションも提供しています。一例として、

:助け

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView beginUpdates];  
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
    // Do whatever data deletion you need to do... 
    // Delete the row from the data source 
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop]; 
    }  
    [tableView endUpdates]; 
} 

希望。

+0

あなたに感謝する方法を知りません.... – WaaleedKhan

+1

withRowAnimation:はい間違っている、あなたは列挙型を使用する必要があります。 withRowAnimation:UITableViewRowAnimationTop – ozba

+0

'... UITableViewRowAnimationTop];'を '... UITableViewRowAnimationAutomatic];'に変更することを検討してください。これは無効な編集(コメントだったはずです)で行われたので、私はそれを元に戻しました。これが正しい場合は、それに応じてソリューションを更新することを検討してください。 –

2

これを行うサンプルアプリケーションがあることはほぼ肯定的です。もっと具体的な答えがすぐに来るでしょう。

更新:iPhoneCoreDataRecipesは、おそらくあなたが探しているものを正確に示します。トピックの

、ここに甘い提供方法の一つである:

// If I want to delete the next 3 cells after the one you click 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSMutableArray* indexPaths = [NSMutableArray array]; 
    for (int i = indexPath.row + 3; i < indexPath.row + 3; i++) 
    { 
    [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]]; 
    } 

    [tableView beginUpdates]; 
    [tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade]; 
    [tableView endUpdates]; 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

私はあなたが望むものを達成する簡単な方法があると思いますが、その後、再び、これは非常に簡単です。唯一の難しさは、あなた自身を削除することかもしれません...セグメンテーションを注意深く見てください。スワイプを得るために

関連する問題