左滑功能和多选功能(在一些列表都会用到的功能)

 

左滑功能

左划删除

1.实现UITableViewDelegate协议和代理
2.实现左划删除功能和修改按钮文字的代理方法
注意:此时按钮没有反应,下面第一个方法可以实现对按钮的监听事件,就可以做出操作
/**
 *  重写这个方法,就可以实现左划删除功能
 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 删除模型数组第一个东西
    // 1.修改模型
    [self.wineArray removeObjectAtIndex:indexPath.row];
    // 2.刷新数据
    [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationAutomatic];
}
/**
 *  重写实现左划删除为中文格式,可以直接设置(下面附图)
 */
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

自定义左划按钮

>和上面一样,实现tableview的协议和代理
>实现tableView对应的代理方法

注意:实现了下面的方法,那么上面的修改文字功能就不实现,在ios9之后上面的第一个方法也不实现,
    左划出现按钮tableview就进入了编辑模式,所以要它退出左划就直接设置退出编辑模式
    self.tableview.editing = NO;
/**
 *  自定义左移出现的按钮
 */
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView 
    editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.tableView.editing = YES;
    
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
     title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        /************** 删除功能 ***************/
        
        // 修改模型
        [self.wineArray removeObject:self.wineArray[indexPath.row]];
        // 刷新表格
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }];
    
    UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal
     title:@"关注" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        /************ 退出编辑模式 *************/
        
        self.tableView.editing = NO;
    }];
    
    return @[action1, action2]; // 越先添加,按钮越靠近右边
}

多选情况

多选

    self.tableView.allowsMultipleSelection = YES;

编辑模式下多选

    self.tableView.allowsMultipleSelectionDuringEditing = YES;

事例:要批量删除,然后整体删除

    self.tableview.indexPathsForSelectedRows // 可以保持选中的行
    注意:数组不能遍历的同时删除,会删错东西

代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 编辑模式下的多选
    self.tableView.allowsMultipleSelectionDuringEditing = YES;
}
/**
 *  批量删除
 */
- (IBAction)deleteAll { // 实现点击换button功能
    /**** 根据编辑模式设置可选状态 ****/
    [self.tableView setEditing:!self.tableView.editing animated:YES];
    self.deleteAllBtn.selected = self.tableView.editing;
}
/**
 *  删除
 */
- (IBAction)delete {
    
    /***** 1.找出要删除的行 *****/
    NSMutableArray *deleteArray = [NSMutableArray array];
    // self.tableView.indexPathsForSelectedRows 选中的行
    NSArray *rowsArray = self.tableView.indexPathsForSelectedRows;
    for (NSIndexPath *indexPath in rowsArray) {
        [deleteArray addObject:self.wineArray[indexPath.row]];
    }
    
    /***** 2.修改模型 *****/
    [self.wineArray removeObjectsInArray:deleteArray];
    
    /***** 3.刷新表格 *****/
    [self.tableView deleteRowsAtIndexPaths:rowsArray 
        withRowAnimation:UITableViewRowAnimationAutomatic];
}

转载于:https://www.cnblogs.com/LongLJ/p/5002044.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值