//cell一键全选和一键全不选
-(void)allSelectClick:(UIButton *)button
{
self.indexPaths = [[NSMutableArray alloc]init];
if (self.isAllSelect) {
for (NSInteger i = 0; i < self.dataSourceAry.count; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
Entity *entity = self.dataSourceAry[indexPath.row];
[self.indexPaths addObject:entity];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
button.selected = YES;
self.isAllSelect = NO;
}else{
for (NSInteger i = 0; i < self.dataSourceAry.count; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
Entity *entity = self.dataSourceAry[indexPath.row];
[self.indexPaths removeObject:entity];
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
button.selected = NO;
self.isAllSelect = YES;
}
}
//删除按钮
-(void)deleteClick
{
// [self.dataSourceAry removeObjectsInArray:self.indexPaths];
for (Entity *entity in self.indexPaths) {
[self.dataSourceAry removeObject:entity];
[Dao.sharedInstance remove:entity];
}
[self.tableView reloadData];
[self bottomDisAppear];
if (self.dataSourceAry.count == 0) {
self.navigationItem.rightBarButtonItem = nil;
}
}
//消除cell选中
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
Entity *entity = self.dataSourceAry[indexPath.row];
if (tableView.editing) {
//NSArray *indexPaths = [tableView indexPathsForSelectedRows];
[self.indexPaths removeObject:entity];
if (self.indexPaths.count < self.dataSourceAry.count) {
UIButton *allSelectButton = (UIButton *)[self.view viewWithTag:AllSelectButonTag];
allSelectButton.selected = NO;
self.isAllSelect = YES;
}
}
}
//选中cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Entity *entity = self.dataSourceAry[indexPath.row];
//NSArray *indexPaths = [tableView indexPathsForSelectedRows];
[self.indexPaths addObject:entity];
if (self.indexPaths.count == self.dataSourceAry.count) {
UIButton *allSelectButton = (UIButton *)[self.view viewWithTag:AllSelectButonTag];
allSelectButton.selected = YES;
self.isAllSelect = YES;
[self allSelectClick:allSelectButton];
}
}
转载于:https://my.oschina.net/u/2430056/blog/519899