1. 程式人生 > >iOS開發,tableView側滑刪除的實現,或者新增多個側滑顯示按鈕

iOS開發,tableView側滑刪除的實現,或者新增多個側滑顯示按鈕

如果想要實現簡單的刪除操作,1 + 2 + 4方法就可以實現 ,如果你要滑動出現多個操作按鈕的話,就要實現方法5了,第五個方法說白了就是集合了前幾個方法,而且可以新增多個按鈕,如果用第5個方法,1、2、4方法可以不實現

//1
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
//2
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete;
}
//3
//修改編輯按鈕文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"刪除";
}
//4
//點選刪除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    //在這裡實現刪除操作
    
    //刪除資料,和刪除動畫
//    [self.myarray removeObjectAtIndex:deleteRow];
//    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:deleteRow inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
}
//5
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    //刪除
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
      
    }];
    //編輯
    UITableViewRowAction *editRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"編輯" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
       
        
    }];
    return @[deleteRowAction,editRowAction];
}