美文网首页tableview和cell相关
UITableViewCell左滑删除、批量编辑

UITableViewCell左滑删除、批量编辑

作者: 游某人 | 来源:发表于2016-06-24 23:46 被阅读1021次

实现cell的左滑删除

  • 只需要遵守tableView的代理,实现以下方法即可实现
//左滑编辑模式
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //可在此对点击cell右边出现的按钮进行逻辑处理
}

//设置左滑删除按钮的文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
  //设置右边按钮的文字
    return @"删除";
}
  • 实现以下方法可代替以上两个方法,还能添加多个按钮,但只适配到iOS 8.0
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"关注" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        NSLog(@"点击了关注");
        tableView.editing = NO;
        
    }];
    
    UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        //        NSLog(@"点击了删除");
        [self.wineArray removeObjectAtIndex:indexPath.row];
        NSIndexPath *newPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
        [tableView deleteRowsAtIndexPaths:@[newPath] withRowAnimation:UITableViewRowAnimationFade];
        
    }];
    
    return @[action1,action2];
}
  • 左滑出现两个按钮


    Snip20160624_1.png
  • 实现批量处理

//先设置tableView的多选属性
//设置多选
    self.tableView.allowsMultipleSelectionDuringEditing = YES;

//再在点击事件中实现逻辑
- (IBAction)editingModel {
    [self.tableView setEditing:!self.tableView.editing animated:YES];
}
  • 如图


    Snip20160624_2.png
  • 还有自定义cell批量删除以后有机会再讲

相关文章

网友评论

  • 2f766dce1076:自定义cell批量删除写了吗? 求大神给力.
    游某人:@鸢尾染半夏 重写系统的UITableViewCellEditControl,替换选中和未选中的图片,图片就和系统不一样了,在自定义cell里layoutsubview里面替换系统图片
    2f766dce1076:@游某人 - -我怎么感觉自定义和系统的没区别啊
    游某人:@鸢尾染半夏 :sweat_smile:你先网上搜搜,很多的,大同小异,我最近都没时间:smile:,然鹅方法都是一样的。
  • 洁简:左滑按钮大小能改变吗
  • feng_dev:太厉害了大神,喜欢

本文标题:UITableViewCell左滑删除、批量编辑

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