手指在单元格上滑动的委托方法

时间:2010-12-16 06:18:00

标签: iphone uitableview cocoa-touch uikit swipe

是否有任何委托方法称为表格单元格中的一次滑动,右侧是否有删除按钮?

我想跟踪滑动并在那里做一些动作。

此外,当您点击单元格中的“删除”按钮时,会调用哪个委托方法。

alt text

1 个答案:

答案 0 :(得分:6)

在行进入“编辑”模式之前,将调用UITableViewDelegate方法tableView:editingStyleForRowAtIndexPath:。当您滑动单元格以及表格视图收到setEditing:animated消息时,会调用此方法。如果您有一个将表格视图置于编辑模式的编辑按钮,则需要知道将为每个可见单元格调用它。

所以你可以这样做:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView 
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.editing) {
        return UITableViewCellEditingStyleDelete;
    }
    else {
        // do your thing
        return UITableViewCellEditingStyleDelete;
    }
}

当您点按“删除”按钮时,tableView:commitEditingStyle:forRowAtIndexPath:会被调用。

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

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // repsond to delete
    }     
}

如果您想更改删除按钮的文字,可以使用tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:

如果在另一边你不想显示删除按钮但是做其他事情你应该查看UISwipeGestureRecognizer并自己处理它。