使用单行/单元格进入编辑模式

时间:2013-03-18 03:29:22

标签: iphone objective-c editmode

我有一个来自此示例的自定义滑动识别类: How to detect a swipe-to-delete gesture in a customized UITableviewCell?

    - (void)cellSwiped:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        UITableViewCell *cell = (UITableViewCell *)gestureRecognizer.view;
        NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
    }
}

现在我想在indexPath中选择那一行并启用它的编辑模式,有人可以告诉我如何做到这一点吗?

3 个答案:

答案 0 :(得分:2)

在某个地方缓存您想要的行,并在- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath的实现中缓存,只返回YES您想要编辑的行。然后通过发送[tableView setEditing:YES animated:YES];进入编辑模式。

答案 1 :(得分:0)

您的班级中可能有NSIndexPath *index的变量,然后有

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath == self.index;
}

并将索引设置为您希望能够编辑的任何单元格,然后在您想要编辑该单元格时调用tableView setEditing:YES animated:YES/NO

答案 2 :(得分:0)

试试这个,

在tableView didSelectRowAtIndex委托方法中调用editingStyleForRowAtIndexPath方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 [yourTableView.delegate tableView:tableView editingStyleForRowAtIndexPath:indexPath];
}

editingStyleForRowAtIndexPath方法中,

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {


  return UITableViewCellEditingStyleDelete;

  }


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



}
相关问题