打开编辑模式,使得tableview处于选择状态
[_tableView setEditing:YES animated:NO]
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
添加 和 取消 选中项
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 添加选中删除项<span style="font-family: Menlo;"> </span>
NSUInteger row = [indexPath row];
id addObject = [_dataArray objectAtIndex:row];
[_selectArray addObject:addObject];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 取消选中删除项
NSUInteger row = [indexPath row];
id addObject = [_dataArray objectAtIndex:row];
[_selectArray removeObject:addObject];
}
选中时候默认圆圈蓝色打勾
我是在自定义cell中重写,在原本蓝色打勾图片上覆盖白色背景处理
[self.tableView setEditing:YES animated:NO];
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
if (editing)//编辑状态
{
if (self.editingStyle == (UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete)){ //编辑多选状态
// 是否选中当前行
UIImage *imgNO = [UIImage imageNamed:@"icon_xwsc_no"];
UIImage *imgYes = [UIImage imageNamed:@"icon_xwsc_yes"];
[_btnEditing setImage:imgNO forState:UIControlStateNormal];
[_btnEditing setImage:imgYes forState:UIControlStateSelected];
_imgView.tag = 100;
[_imgView addSubview:_btnEditing];
[self addSubview:_imgView];
}
}else { //非编辑模式下检查是否有打勾图片,有的话删除
UIView *editDotView = [self viewWithTag:100];
if (editDotView)
{
[editDotView removeFromSuperview];
}
}
}
<p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><pre name="code" class="html"><span style="color:#333333;">- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// 编辑状态下选中行
if (selected && self.editing) {
_btnEditing.selected = YES; // 当前行处于选择状态
}else{
_btnEditing.selected = NO; // 取消当前行选择状态
}
}</span>
回到vc中做删除处理
- (void)btnSelect:(UIButton *)btn
{
int count = [_selectArray count];
if (count > 0 ) { // 有选中删除项
[_dataArray removeObjectsInArray:_selectArray];//删除已经勾选的数据
[_tableView reloadData]; //重新加载
[_selectArray removeAllObjects];//清空已经勾选了的数据列表
}
}