表视图在indexPath处为选定单元格设置setEditing

时间:2012-12-14 04:56:53

标签: objective-c xcode uitableview setediting

我希望在长按事件的选定索引处为表视图中的单个单元格启用编辑,除了可以编辑整个表格外,一切正常。如何仅在选定的单元格上启用编辑?

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {

    CGPoint p = [gestureRecognizer locationInView:self.savedPropertyTableView];

    NSIndexPath *indexPath = [self.savedPropertyTableView indexPathForRowAtPoint:p];
            if (indexPath == nil) {
            [self setEditing:YES animated:YES];
            NSLog(@"long press on table view but not on a row");
        }

        else {
            [self setEditing:YES animated:YES];
            NSLog(@"long press on table view at row %d", indexPath.row);
        } 
    }

2 个答案:

答案 0 :(得分:0)

而不是整个表格使单个单元格可编辑:

UITableViewCell *newCell = [yourTable cellForRowAtIndexPath:indexPath];

[newCell setEditing:YES animated:YES];

答案 1 :(得分:0)

不是一个确切的解决方案,但结果相同,我已经创建了一个滑动手势,就像我希望的那样(删除单个单元格)

在表视图单元格中:

UISwipeGestureRecognizer *sgdr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSlideRight:)];
[sgdr setDirection:UISwipeGestureRecognizerDirectionRight];
[self.savedPropertyTableView addGestureRecognizer:lpgr];

然后创建方法:

-(void)handleSlideRight:(UISwipeGestureRecognizer *)gestureRecognizer {
    CGPoint p = [gestureRecognizer locationInView:self.savedPropertyTableView];

    NSIndexPath *indexPath = [self.savedPropertyTableView indexPathForRowAtPoint:p];
    if (indexPath == nil)
    {
        [self setEditing:YES animated:YES];
        NSLog(@"slide on table view but not on a row");
    }

    else
    {
        [self setEditing:YES animated:YES];
        NSLog(@"slide on table view at row %d", indexPath.row);
    }
}