ios11 tableview slide left to the end disable delete

 

After iOS8, Apple officially added UITableVIew's right slide operation interface, that is, a new proxy method (tableView: editActionsForRowAtIndexPath:) and a class (UITableViewRowAction) are added. The proxy method returns an array. We can use this proxy method. Define the required operation buttons (delete, put on top, etc.), the class of these buttons is UITableViewRowAction

/**
 *  设置UITableView 进入编辑状态的样式
 */
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*
     UITableView 编辑状态的样式有如下三种:
        UITableViewCellEditingStyleNone:cell往右缩进,但是左边不出现任何控件
        UITableViewCellEditingStyleDelete:cell往右缩进,但是左边出现红色减号控件
        UITableViewCellEditingStyleInsert:cell往右缩进,但是左边出现蓝色加号控件
        UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert:cell往右缩进,但是左边出现选择控件
     */
    if (tableView.editing) {
        return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;
    }
    else
    {
        return UITableViewCellEditingStyleDelete;
    }
}

Custom delete button

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //添加一个删除按钮
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        //处理数据
        [self.dataArr removeObjectAtIndex:indexPath.row];
        //更新UI
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }];
    //放回数组返回
    return @[deleteAction];
}

 

There have been some changes since iOS 11. First, you can add pictures to these buttons, and then if the following two new proxy methods for iOS 11 are implemented, they will replace the (tableView: editActionsForRowAtIndexPath:) proxy method:

// Swipe actions
// These methods supersede -editActionsForRowAtIndexPath: if implemented
// return nil to get the default swipe actions
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);

 

When creating an UIContextualActionobject, UIContextualActionStylethere are two types. If it is the top button, read button, etc., use the UIContextualActionStyleNormaltype, and the delete operation button can use the UIContextualActionStyleDestructivetype. When using this type, if it is a right slide operation, slide a cell to the right, it will be directly To perform the delete operation, there is no need to click the delete button, but it is possible that we do not want to delete, and we delete it by mistake when we swipe to the end. Press the following implementation to solve it.

Tableview slide left to the end disable delete

- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos){// delete action
	UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction * _Nonnull action,__kindof UIView * _Nonnull sourceView,void (^ _Nonnull completionHandler)(BOOL)) {
[tableView setEditing:NO animated:YES];// 这句很重要,退出编辑模式,隐藏左滑菜单
[self.dataArr removeObjectAtIndex:indexPath.row];
[_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
[_tableView reloadData];
		completionHandler(true);
}];

	UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction]];
	actions.performsFirstActionWithFullSwipe = NO;
	return actions;

}

 

 

 

Guess you like

Origin blog.csdn.net/zjpjay/article/details/91378802