#pragma mark - 选中某行的点击操作
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.tableView.editing) {
[self.selectArr addObject:self.modelArr[indexPath.row]];
}else{
[tableView deselectRowAtIndexPath:indexPath animated:YES];// 取消选中
//跳转界面
}
}
//当取消删除时,要将Arr中的数据移除,不然会造成 (你先选中一行 然后取消选中 但是当你点击删除按钮时,这行cell还是会被删除)
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.selectArr removeObject:self.modelArr[indexPath.row]];
}
#pragma mark 返回编辑模式,默认为删除模式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
#pragma mark - 删除
- (void)DeleteCell{
[self.tableView setEditing:!self.tableView.isEditing animated:YES];
// 允许在编辑模式进行多选操作
self.tableView.allowsMultipleSelectionDuringEditing = YES;
//点击删除
if (self.tableView.editing) {
}else{
//点击完成
[self.modelArr removeObjectsInArray:self.selectArr];
[self.selectArr removeAllObjects];
[self.tableView reloadData];
}
}
自定义cell的设置
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
[self setBGColor];
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
[super setHighlighted:highlighted animated:animated];
[self setBGColor];
}
-(void)setBGColor{
//可以隐藏选中状态的cell的分割线
for (UIView *view in self.subviews) {
if ([view isMemberOfClass:NSClassFromString(@"_UITableViewCellSeparatorView")]) {
view.hidden=YES;
}
}
//选中状态会把控件变成清除色,重新设置
self.lineView.backgroundColor = [UIColor whiteColor];
//取消选中之后的背景颜色
self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame];
self.selectedBackgroundView.backgroundColor = [UIColor clearColor];
//通过这个可更换多选模式,蓝色对号的颜色
self.tintColor = [UIColor redColor];
}