简体   繁体   中英

How to delete the tableview cell without pressing delete button?

 - (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];
}

you can add swipe gesture to table and delete row as shown following.

- (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];
}

使用带有动画Github代码的 滑动表格单元格检查此代码

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM