How to enable swipe to delete cell in a TableView?

后端 未结 13 2666
悲哀的现实
悲哀的现实 2020-12-07 21:40

I have a UIViewController that implements TableViews delegate and datasource protocols. Now I want to add \"swipe to delete\" gesture to cells.

13条回答
  •  我在风中等你
    2020-12-07 22:23

    NSMutableArray *post= [NSMutableArray alloc]initWithObject:@"1",@"2",@"3",nil]; 
    
    
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView 
               editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        NSUInteger row = [indexPath row];
        NSUInteger count = [posts count];
    
        if (row < count) {
            return UITableViewCellEditingStyleDelete;
        } else {
            return UITableViewCellEditingStyleNone;
        }
    }
    
    - (void)tableView:(UITableView *)tableView 
                        commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
                        forRowAtIndexPath:(NSIndexPath *)indexPath {
    
        NSUInteger row = [indexPath row];
        NSUInteger count = [posts count];
    
        if (row < count) {
    
            [posts removeObjectAtIndex:row];
        }
    }
    

提交回复
热议问题