关于tableview 编辑下多选择处理

来源:互联网 发布:数据化分析 编辑:程序博客网 时间:2024/05/16 15:02


1.设置可以多选择(并且设置tintColor 可以改变右侧 符号颜色)

  self.tableView.allowsMultipleSelectionDuringEditing = YES;

  self.tableView.tintColor =MAIN_THEME_GREEN_COLOR;


效果图:



2.定义下面修改状态的toolView

  

    UIView *spliter = [UIViewnew];

    spliter.backgroundColor =NORMAL_SPLITER_GREY;

    [self.toolViewaddSubview:spliter];

    [spliter mas_makeConstraints:^(MASConstraintMaker *make) {

        make.width.mas_equalTo(self.toolView);

        make.height.mas_equalTo(0.5);

        make.left.mas_equalTo(0);

        make.top.mas_equalTo(0);

    }];

    

    

    self.changeSignBtn = [UIButtonnew];

    self.changeSignBtn.titleLabel.font = BIG_FONT;

    [self.changeSignBtnsetTitle:@"签到"forState:UIControlStateNormal];

    [self.changeSignBtnsetTitleColor:MAIN_THEME_GREEN_COLORforState:UIControlStateNormal];

    [self.changeSignBtnaddTarget:selfaction:@selector(batchChange:)forControlEvents:UIControlEventTouchUpInside];

    [self.toolViewaddSubview:self.changeSignBtn];

.....

    

    self.changeLateBtn = [UIButtonnew];

    self.changeLateBtn.titleLabel.font = BIG_FONT;

    [self.changeLateBtnsetTitle:@"迟到"forState:UIControlStateNormal];

    [self.changeLateBtnsetTitleColor:MAIN_THEME_GREEN_COLORforState:UIControlStateNormal];

    [self.changeLateBtnaddTarget:selfaction:@selector(batchChange:)forControlEvents:UIControlEventTouchUpInside];

    [self.toolViewaddSubview:self.changeLateBtn];

.....

    

    self.changeEarlyBtn = [UIButtonnew];

    self.changeEarlyBtn.titleLabel.font = BIG_FONT;

    [self.changeEarlyBtnsetTitle:@"早退"forState:UIControlStateNormal];

    [self.changeEarlyBtnsetTitleColor:MAIN_THEME_GREEN_COLORforState:UIControlStateNormal];

    [self.changeEarlyBtnaddTarget:selfaction:@selector(batchChange:)forControlEvents:UIControlEventTouchUpInside];

    [self.toolViewaddSubview:self.changeEarlyBtn];

.....

    

    

    self.changeUnsignBtn = [UIButtonnew];

    self.changeUnsignBtn.titleLabel.font = BIG_FONT;

    [self.changeUnsignBtnsetTitle:@"未签到"forState:UIControlStateNormal];

    [self.changeUnsignBtnsetTitleColor:MAIN_THEME_GREEN_COLORforState:UIControlStateNormal];

......



3.右键编辑状态事件并且给toolview添加动画

-(void)showToolBar:(id)sender{

    if (self.tableView.isEditing) {

        [self.navigationItem.rightBarButtonItemsetTitle:@"编辑"];

        [self.tableViewsetEditing:NOanimated:YES];

        

        [UIViewbeginAnimations:nilcontext:nil];

        [UIViewsetAnimationDuration:0.3];

        [self.toolViewmas_updateConstraints:^(MASConstraintMaker *make) {

            make.top.mas_equalTo(self.view.mas_bottom);

        }];

        [self.viewlayoutIfNeeded];

        [UIViewcommitAnimations];

        

        self.selectedStatisticArray = [NSMutableArray<TchStdSignDetail*>new];

        

    }else{

        [self.navigationItem.rightBarButtonItemsetTitle:@"完成"];

        [self.tableViewsetEditing:YESanimated:YES];

        

        [UIViewbeginAnimations:nilcontext:nil];

        [UIViewsetAnimationDuration:0.3];

        [self.toolViewmas_updateConstraints:^(MASConstraintMaker *make) {

            make.top.mas_equalTo(self.view.mas_bottom).offset(-40);

        }];

        [self.viewlayoutIfNeeded];

        [UIViewcommitAnimations];

        

        self.selectedStatisticArray = [NSMutableArray<TchStdSignDetail*>new];

    }

}


4.勾选添加删除的逻辑

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

if (tableView.editing) {

      .......省略.......(statistic 是Model数据)

        if([self.selectedStatisticArraycontainsObject:statistic]){

            [self.selectedStatisticArrayremoveObject:statistic];

        }else{

            [self.selectedStatisticArrayaddObject:statistic];

        }

    }else{

        [tableView deselectRowAtIndexPath:indexPathanimated:NO];

    }


}





0 0
原创粉丝点击