2012-01-30 9 views

答えて

6

私はこれを試して実装していませんが、私はそれを撃つでしょう。最初のtableViewでセルを見つけること。(注意、アンindexPathを、カスタムのUITableViewCellを作成し、それはあなたが

  1. それはで使用されているのtableViewへの参照を使用することができます2つの性質を持ってみましょう。

cellForRowAtIndexPath:では、カスタムセルを作成する場所でこれらのプロパティを設定します。また、セルにUISwipeGestureRecognizerを追加してください。

cell.tableView=tableView; 
cell.indexPath=indexPath; 
UISwipeGestureRecognizer *swipeGestureRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(deleteCell:)]; 
[cell addGestureRecognizer:swipeGestureRecognizer]; 
[swipeGestureRecognizer release]; 

ジェスチャーが水平スワイプのみを受け取るようにしてください。

-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 
{ 
    if([[gestureRecognizer view] isKindOfClass:[UITableViewCell class]]&& 
     ((UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionLeft 
     ||(UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionRight)) return YES; 
} 

あなたdeleteCell:

-(void) deleteCell:(UIGestureRecognizer*)gestureRec 
{ 
    UIGestureRecognizer *swipeGestureRecognizer=(UISwipeGestureRecognizer*)gestureRec; 
    CustomCell *cell=[swipeGestureRecognizer view]; 
    UITableView *tableView=cell.tableView; 
    NSIndexPath *indexPath=cell.indexPath; 
    //you can now use these two to perform delete operation 
} 
+0

ジェスチャ認識装置の向きを最初に初期化してセルに追加する方向を設定する必要があることを忘れないでください。このようなもの:swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight; – ColossalChris

+0

Apple、 '(void)cancelPreviousPerformRequestsWithTarget:(id)aターゲットセレクタ:(SEL)aSelector'を提供していないあなた。 –

-2

あなたのプロジェクトにこのコードを追加する必要があります。

// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return YES if you want the specified item to be editable. 
    return YES; 
} 

// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     //add code here for when you hit delete 
    }  
} 
エルス

MOR情報uがこのリンクを介して enter link description here

+0

Vivek2012とiWill - お返事ありがとうございますが、DELETEボタンを表示したくありません。すでにstackoverflowの回答があります。 スワイプの左または右のジェスチャーを受信した分、セルを消去します。私の説明を読んでくださいありがとう – carbonr

-1

を行くことができるUITableViewCellEditingStyleがUITableViewCellEditingStyleDeleteであれば、その後、

であなたのセルを消去するためにSTHを行う、これらの2つの方法

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath; 

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

を実装

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; 

それだけです。

3

で@MadhavanRPによって掲示ソリューションは動作しますが、それは必要以上に複雑です。より簡単なアプローチを取って、表内で発生するすべてのスワイプを処理し、スワイプの位置を取得してユーザーがスワイプしたセルを特定するジェスチャ認識機能を作成できます。セットアップするには

ジェスチャー認識:スワイプを処理するためにviewDidLoad

- (void)setUpLeftSwipe { 
    UISwipeGestureRecognizer *recognizer; 
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self 
                 action:@selector(swipeLeft:)]; 
    [recognizer setDirection:UISwipeGestureRecognizerDirectionLeft]; 
    [self.tableView addGestureRecognizer:recognizer]; 
    recognizer.delegate = self; 
} 

コールこの方法:

- (void)swipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer { 
    CGPoint location = [gestureRecognizer locationInView:self.tableView]; 
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location]; 
    ... do something with cell now that i have the indexpath, maybe save the world? ... 
} 

注:あなたのVCがジェスチャ認識のデリゲートを実装する必要があります。

関連する問題