美文网首页
tableview cell 侧滑删除& 多选 2020-05-

tableview cell 侧滑删除& 多选 2020-05-

作者: iOS打怪升级 | 来源:发表于2020-05-11 16:54 被阅读0次
  1. 需要用到的方法
//设置是否可以编辑,只有允许才会出现系统提供的那些特性- cell 展示的时候会调用
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.isEditing) {
      // 如有需要这里也可以差异化处理场景,如已下载的视频肯定不允许再次选中,这里会返回NO 
     
    }
    return YES;
}

//侧滑的时候会触发这个方法 1
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
   // 显示选择按钮,注意这个组合必须这么用才会出现选择按钮
    if (未下载) {
        return  UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;
    }else{
   // 已下载-显示删除选项
        return UITableViewCellEditingStyleDelete; 
    }
}

//配置侧滑选项名称,背景色-侧滑的时候会触发这个方法 2
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    @weakify(self);
    UITableViewRowAction * action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        @strongify(self);
        DDLogDebug(@"删除下载cell event");
    }];
    action.backgroundColor = HexRGB(0xEC5C5C);
    return @[action];
}





ios 11.0 及以上会有左滑自动触发删除特性,低版本<ios 8+>需要手动点击触发删除事件

相关文章

网友评论

      本文标题:tableview cell 侧滑删除& 多选 2020-05-

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