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

来源:互联网 发布:屏幕录像软件 编辑:程序博客网 时间:2024/04/29 22:30

-(void)editAction:(UIButton*)sender

{


    [self.tableViewsetEditing:YESanimated: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) {

            

        }

    [selfaddAllSelectView];

    }

    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

{

    returnUITableViewCellEditingStyleDelete;//|UITableViewCellEditingStyleInsert;

}

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

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

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

    if (editingStyle==UITableViewCellEditingStyleDelete)

    {

        [SVProgressHUDshow];

             if (self.DeleteCollectionBlock)

        {

            self.DeleteCollectionBlock();

        }

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

        NSString *code=[NSStringmd5:codeString];

   // MD5加密

        NSString *urlString=[NSStringstringWithFormat:、xxxxxxxxxxxxxxxxxxx];

        NSMutableDictionary *params = [NSMutableDictionarydictionary];

        params[@"user_id"] = UserID;

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

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


        [[UIApplicationsharedApplication]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=[NSIndexPathindexPathForItem:dataArray.count-1inSection:0];

        

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

    }

}

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

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

{

    for (UIView *viewin cell.subviews) {

        if ([viewisKindOfClass:NSClassFromString(@"UITableViewCellEditControl")])

        {

            for (UIView *vVVin view.subviews) {

                NSLog(@"%@",vVV);

                [vVV removeFromSuperview];

            }

            WXButton* checkButton= [selfcheckBox: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:selfaction:@selector(checkButton:)forControlEvents:UIControlEventTouchUpInside];

            for (NSDictionary *temDict1in _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 = [NSMutableArrayarrayWithArray:_selectArray];

    for (NSDictionary *tempDicin _selectArray)

    {

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

    {

     [tempArray removeObject:tempDic];

    }

    _selectArray = tempArray;

    }

    }

}


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





0 0
原创粉丝点击