关于tableView多选删除的编辑设置

-(void)editAction:(UIButton*)sender

{


    [self.tableView setEditing:YES animated:YES];

    sender.selected=!sender.selected;

    NSLog(@"%d",sender.selected);

    [sender setTitle:sender.selected?@"完成":@"编辑" forState:UIControlStateNormal];

    [_tableView reloadData];

    if (sender.selected)

    {

        self.tableView.editing=YES;

        if (self.tableView.editing==YES) {

            

        }

    [self addAllSelectView];

    }

    if (!sender.selected) {

        self.tableView.editing=NO;

        self.tableView.frame = CGRectMake(0, 0, iPhoneWidth, iPhoneHeigt);

        self.editView.hidden = YES;

    }

}

点击按钮   使得cell进入编辑状态,editView 上面放的是勾选之后的删除按钮

// 设置tableView是否可以删除

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    return YES;

}

//   设置删除操作时候的标题

-(NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{

    return @"删除";

}

//问题2:某一行支持哪种编辑模式

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return UITableViewCellEditingStyleDelete;//|UITableViewCellEditingStyleInsert;

}

tableView进入编辑状态,左滑删除

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"%ld",(long)editingStyle);

    if (editingStyle==UITableViewCellEditingStyleDelete)

    {

        [SVProgressHUD show];

             if (self.DeleteCollectionBlock)

        {

            self.DeleteCollectionBlock();

        }

        NSString *codeString=[NSString stringWithFormat:@"%@-%@",UserID,KEY];//(md5(account-key))

        NSString *code=[NSString md5:codeString];

   // MD5加密

        NSString *urlString=[NSString stringWithFormat:、xxxxxxxxxxxxxxxxxxx];

        NSMutableDictionary *params = [NSMutableDictionary dictionary];

        params[@"user_id"] = UserID;

        params[@"act"] = @"delete";

        params[@"rec_id"] = [dataArray[indexPath.row] objectForKey:@"rec_id"];


        [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];

        NSLog(@"%@",urlString);

        [WXNetworking WXBaseNetworkWithPOST:xxxxxx params:params success:^(id responseObject) {

          

//         删除成功后移除数据源并刷新tableView

        [dataArray removeObjectAtIndex:indexPath.row];



            [tableView endUpdates];

            

            [tableView reloadData];

            

        } failure:^(NSError *error) {

            

        }];

    

    

    }

    if (editingStyle == UITableViewCellEditingStyleInsert) {

        NSIndexPath *newIP=[NSIndexPath indexPathForItem:dataArray.count-1 inSection:0];

        

        [tableView insertRowsAtIndexPaths:@[newIP] withRowAnimation:UITableViewRowAnimationAutomatic];

    }

}

因为cell的复用的关系,所以要对cell的选中按钮做判断,在从视图中从新出现的时候要判断之前是否再选中组里面    同样是遍历数组的方式来判断。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

    for (UIView *view in cell.subviews) {

        if ([view isKindOfClass:NSClassFromString(@"UITableViewCellEditControl")])

        {

            for (UIView *vVV in view.subviews) {

                NSLog(@"%@",vVV);

                [vVV removeFromSuperview];

            }

            WXButton* checkButton= [self checkBox:CGRectMakeE(0,0,47,110)];

            NSLog(@"%ld",(long)indexPath.row);

            NSInteger row=indexPath.row;

            int ii=(int)row;

            NSLog(@"%d",ii);

            checkButton.hang=ii;

            [checkButton addTarget:self action:@selector(checkButton:) forControlEvents:UIControlEventTouchUpInside];

            for (NSDictionary *temDict1 in _selectArray)

            {

            if (dataArray[indexPath.row] == temDict1) {

                    checkButton.selected = YES;

            }else{

            }

                

            }


            [view addSubview:checkButton];

        }

    }

    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {

        [cell setSeparatorInset:UIEdgeInsetsZero];

    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {

        [cell setLayoutMargins:UIEdgeInsetsZero];

    }

    cell.preservesSuperviewLayoutMargins = NO;

}




// 多选时选中取消的操作

// 自定义的选中按钮   对选中按钮的勾选做对删除分组的操作

-(void)checkButton:(WXButton*)sender{

    

    sender.selected=!sender.selected;


    if (sender.selected==YES)

    {

    model = self.modelArray[sender.hang];

    model.isSelect = YES;

    [_selectArray addObject:dataArray[sender.hang]];

    }

    if (sender.selected == NO)

    {

//遍历删除数组 对所勾选的商品做出操作  _selectArray是要删除tableViewcell的数组  遍历对比选中的数组   tempArray 的使用是因为一个数组不能同事在两个线程做操作。 

    NSMutableArray *tempArray = [NSMutableArray arrayWithArray:_selectArray];

    for (NSDictionary *tempDic in _selectArray)

    {

    if (tempDic == dataArray[sender.hang])

    {

     [tempArray removeObject:tempDic];

    }

    _selectArray = tempArray;

    }

    }

}


由于水平有限,代码还存在可以优化的地方。希望读者提供宝贵的意见。描述不清楚的地方欢迎大家提出。





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值