如何删除tableview单元格而不按删除按钮?

时间:2015-07-14 09:40:14

标签: ios

 - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [tableView1 cellForRowAtIndexPath:indexPath];
    UITableViewRowAction *flagAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

    [tableData removeObjectAtIndex:indexPath.row];
    NSLog(@"indexpath %ld",indexPath.row);
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}];
    return @[ flagAction];
}

2 个答案:

答案 0 :(得分:3)

您可以向表格添加滑动手势并删除行,如下所示。

- (void)viewDidLoad {
    UISwipeGestureRecognizer *gestureDeleteRow = [[UISwipeGestureRecognizer alloc] initWithTarget:self          action:@selector(cellSwipe:)];
    gestureDeleteRow.direction = UISwipeGestureRecognizerDirectionRight;
    [tableView addGestureRecognizer:gestureDeleteRow];
}

//Swipe Handler Method
-(void)cellSwipe:(UISwipeGestureRecognizer *)gesture
{
    CGPoint location = [gesture locationInView:tableView];
    NSIndexPath *swipedIndexPath = [tableView indexPathForRowAtPoint:location];
    //Delete Row…
    [ARRAY removeObjectAtIndex: swipedIndexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:swipedIndexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
}

答案 1 :(得分:0)

使用动画滑动表格单元格检查此代码 Github Code