美文网首页
IOS UITableView中的左划删除等功能

IOS UITableView中的左划删除等功能

作者: tino又想吃肉了 | 来源:发表于2020-07-28 17:22 被阅读0次

左划删除、收藏、置顶

在UITableView中,我们可以实现对cell左划后出现按钮的功能,这一功能常被设计为删除、置顶等功能。而在IOS 11及以上的系统中,我们可以更简便地实现UITableView的左划功能。

下面给出代码

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
    UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        //进行逻辑处理
        [self.tableView reloadData];
    }];
    deleteRowAction.backgroundColor = [UIColor redColor];//按钮的颜色

    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];//以数组的方式添加
    config.performsFirstActionWithFullSwipe = NO;//若返回YES,则将cell划满后会执行第一个Action的方法
    return config;
}

需要注意的是,这个方法是在UITableviewDelegate中的,所以我们的tableview需要遵循UITableviewDelegate和UITableviewDataSource协议
具体操作为:

@interface TableViewController ()<UITableViewDelegate,UITableViewDataSource>

- (void)viewDidLoad {
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

以上便为实现Tableview左划功能的方法

相关文章

网友评论

      本文标题:IOS UITableView中的左划删除等功能

      本文链接:https://www.haomeiwen.com/subject/wqfqrktx.html