您的位置:首页 > 其它

关于tableview 编辑下多选择处理

2016-07-28 17:22 302 查看
1.设置可以多选择(并且设置tintColor 可以改变右侧 符号颜色)

  self.tableView.allowsMultipleSelectionDuringEditing
= YES;
  self.tableView.tintColor =
MAIN_THEME_GREEN_COLOR;

效果图:



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

  
    UIView *spliter = [UIView
new];
    spliter.backgroundColor =
NORMAL_SPLITER_GREY;
    [self.toolView
addSubview: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 = [UIButton
new];
    self.changeSignBtn.titleLabel.font
= BIG_FONT;
    [self.changeSignBtn
setTitle:@"签到"
forState:UIControlStateNormal];
    [self.changeSignBtn
setTitleColor:MAIN_THEME_GREEN_COLOR
forState:UIControlStateNormal];
    [self.changeSignBtn
addTarget:self
action:@selector(batchChange:)
forControlEvents:UIControlEventTouchUpInside];
    [self.toolView
addSubview:self.changeSignBtn];
.....
    
    self.changeLateBtn = [UIButton
new];
    self.changeLateBtn.titleLabel.font
= BIG_FONT;
    [self.changeLateBtn
setTitle:@"迟到"
forState:UIControlStateNormal];
    [self.changeLateBtn
setTitleColor:MAIN_THEME_GREEN_COLOR
forState:UIControlStateNormal];
    [self.changeLateBtn
addTarget:self
action:@selector(batchChange:)
forControlEvents:UIControlEventTouchUpInside];
    [self.toolView
addSubview:self.changeLateBtn];
.....
    
    self.changeEarlyBtn = [UIButton
new];
    self.changeEarlyBtn.titleLabel.font
= BIG_FONT;
    [self.changeEarlyBtn
setTitle:@"早退"
forState:UIControlStateNormal];
    [self.changeEarlyBtn
setTitleColor:MAIN_THEME_GREEN_COLOR
forState:UIControlStateNormal];
    [self.changeEarlyBtn
addTarget:self
action:@selector(batchChange:)
forControlEvents:UIControlEventTouchUpInside];
    [self.toolView
addSubview:self.changeEarlyBtn];
.....
    
    
    self.changeUnsignBtn = [UIButton
new];
    self.changeUnsignBtn.titleLabel.font
= BIG_FONT;
    [self.changeUnsignBtn
setTitle:@"未签到"
forState:UIControlStateNormal];
    [self.changeUnsignBtn
setTitleColor:MAIN_THEME_GREEN_COLOR
forState:UIControlStateNormal];
......

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

-(void)showToolBar:(id)sender{
    if (self.tableView.isEditing)
{
        [self.navigationItem.rightBarButtonItem
setTitle:@"编辑"];
        [self.tableView
setEditing:NO
animated:YES];
        
        [UIView
beginAnimations:nil
context:nil];
        [UIView
setAnimationDuration:0.3];
        [self.toolView
mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.view.mas_bottom);
        }];
        [self.view
layoutIfNeeded];
        [UIView
commitAnimations];
        
        self.selectedStatisticArray = [NSMutableArray<TchStdSignDetail*>
new];
        
    }else{
        [self.navigationItem.rightBarButtonItem
setTitle:@"完成"];
        [self.tableView
setEditing:YES
animated:YES];
        
        [UIView
beginAnimations:nil
context:nil];
        [UIView
setAnimationDuration:0.3];
        [self.toolView
mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.view.mas_bottom).offset(-40);
        }];
        [self.view
layoutIfNeeded];
        [UIView
commitAnimations];
        
        self.selectedStatisticArray = [NSMutableArray<TchStdSignDetail*>
new];
    }
}

4.勾选添加删除的逻辑

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

if (tableView.editing) {
      .......省略.......(statistic 是Model数据)
        if([self.selectedStatisticArray
containsObject:statistic]){
            [self.selectedStatisticArray
removeObject:statistic];
        }else{
            [self.selectedStatisticArray
addObject:statistic];
        }
    }else{
        [tableView deselectRowAtIndexPath:indexPath
animated:NO];
    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: