如何检测自定义的UITableviewCell中的滑动删除手势?

我已经定制了一个UITableViewCell,我想实现“刷卡删除”。 但是我不想要默认的删除button。 相反,我想要做一些不同的事情。 什么是最简单的方法来实现呢? 当用户滑动删除单元格时,是否有一些方法被调用? 我可以防止默认的删除button出现吗?

现在我想我必须实现我自己的逻辑,以避免在UITableViewCell的默认实现中删除默认的删除button和缩放animation。

也许我必须使用UIGestureRecognizer?

如果你想完全不同的东西,添加一个UISwipeGestureRecognizer到每个tableview单元格。

// Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } // Configure the cell. UISwipeGestureRecognizer* sgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwiped:)]; [sgr setDirection:UISwipeGestureRecognizerDirectionRight]; [cell addGestureRecognizer:sgr]; [sgr release]; cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row]; // ... return cell; } - (void)cellSwiped:(UIGestureRecognizer *)gestureRecognizer { if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { UITableViewCell *cell = (UITableViewCell *)gestureRecognizer.view; NSIndexPath* indexPath = [self.tableView indexPathForCell:cell]; //.. } } 

这里有两个方法可以用来避免删除button:

 - (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath 

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