1

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

-(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);
        } 
    }
4

2 回答 2

0

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

UITableViewCell *newCell = [yourTable cellForRowAtIndexPath:indexPath];

[newCell setEditing:YES animated:YES];
于 2012-12-14T06:01:20.117 回答
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);
    }
}
于 2012-12-14T06:02:02.660 回答