ios11テーブルビュースライドを左から最後まで無効にする削除

 

iOS8の後、AppleはUITableVIewの右スライド操作インターフェイスを正式に追加しました。つまり、新しいプロキシメソッド(tableView:editActionsForRowAtIndexPath :)とクラス(UITableViewRowAction)が追加されました。プロキシメソッドは配列を返します。このプロキシメソッドを使用できます。必要な操作ボタン(削除、上に置くなど)を定義します。これらのボタンのクラスは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;
    }
}

カスタム削除ボタン

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

 

iOS 11以降、いくつかの変更があります。まず、これらのボタンに画像を追加できます。次に、iOS 11の次の2つの新しいプロキシメソッドが実装されると、(tableView:editActionsForRowAtIndexPath :)プロキシメソッドが置き換えられます。

// 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);

 

UIContextualActionオブジェクトを作成するときUIContextualActionStyleは、トップボタン、読み取りボタンなどの場合はUIContextualActionStyleNormalタイプを使用し、削除操作ボタンの場合はUIContextualActionStyleDestructiveタイプを使用できます。このタイプを使用する場合、右スライド操作の場合はセルを右にスライドすると直接になります。削除操作を行うために削除ボタンをクリックする必要はありませんが、削除したくない場合があり、最後までスワイプすると誤って削除してしまいます。以下の実装を押して解決してください。

テーブルビューのスライドを左から最後まで無効にして削除

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

}

 

 

 

おすすめ

転載: blog.csdn.net/zjpjay/article/details/91378802