UITableViewCell取消與全選狀態(tài)#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property? (nonatomic, assign)BOOL isAllSelected;
@property (nonatomic,? strong)NSArray? *dataArray;
@property (nonatomic,? strong)UITableView? *tableView;
@property (nonatomic, strong)NSMutableArray *selectedArrM;
@property (strong, nonatomic) NSMutableArray? *selectIndexs; //多選選中的行
//@property (nonatomic, assign)BOOL isSelected;
@end
@implementation ViewController
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? self.title = @"多選cellDemo";
? ? self.selectedArrM = [NSMutableArray array];
? ? self.dataArray = @[@"2",@"3",@"4",@"44565",@"6",@"7",@"8",@"9",@"0",@"13",@"12",@"14",@"15",@"17",@"19",@"20"];
? ? //為左邊的item設(shè)置標(biāo)題和動作
? ? self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"全選" style:UIBarButtonItemStylePlain target:self action:@selector(selecteAllCells:)];
? ? [self customTableView];
}
- (void)customTableView{
? ? self.tableView=? [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
? ? self.tableView.delegate=self;
? ? self.tableView.dataSource=? self;
//? ? self.tableView.editing = YES;
//? ? self.tableView.allowsMultipleSelectionDuringEditing = YES;
? ? //rself.tableView = tableV;
? ? [self.view addSubview:self.tableView ];
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
? ? return self.dataArray.count;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
? ? UITableViewCell? *cell = [tableViewdequeueReusableCellWithIdentifier:@"UITableViewCell"];
? ? if(cell ==nil) {
? ? ? ? cell =? [[UITableViewCell? alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"UITableViewCell"];
? ? }
? ? cell.accessoryType = UITableViewCellAccessoryNone;
? ? for(NSIndexPath*indexinself.selectIndexs) {
? ? ? ? if(index ==? indexPath) {
? ? ? ? ? ? cell.accessoryType? =UITableViewCellAccessoryCheckmark;
? ? ? ? ? ? break;
? ? ? ? }
? ? }
? ? cell.textLabel.text=self.dataArray[indexPath.row];
? ? returncell;
}
//獲取tableView上面所有的cell
- (NSArray*)cellsForTableView:(UITableView*)tableView{
? ? NSIntegersections = tableView.numberOfSections;
? ? NSMutableArray *cells = [[NSMutableArray alloc]init];
?? ? for(intsection =0; section < sections; section++) {
?? ? NSIntegerrows = [tableView? numberOfRowsInSection:section];
?? ? for(introw =0; row < rows; row++) {
? ? NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
?? ? ? ? [cellsaddObject:[tableViewcellForRowAtIndexPath:indexPath]];
? ? ? ? ? }
?? ? }
? ? returncells;
}
#pragma mark Actions
- (void)selecteAllCells:(UIBarButtonItem*)sender {
? ? NSArray *arr = [self.tableView indexPathsForVisibleRows];
? ? if (_isAllSelected == YES) {
? ? ? ? _isAllSelected = NO;
? ? ? ? [sendersetTitle:@"取消"];
? ? ? ? for(NSIndexPath*indexPathinarr) {
? ? ? ? ? ? //根據(jù)索引观游,獲取cell 然后就可以做你想做的事情啦
? ? ? ? ? ? UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
? ? ? ? ? ? cell.accessoryType = UITableViewCellAccessoryNone; //切換為選中
? ? ? ? }
? ? ? ? for(inti =0; i
? ? ? ? ? ? [self.selectedArrM? removeObject:self.dataArray[i]];
? ? ? ? ? ? NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
? ? ? ? ? ? [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
? ? ? ? }
? ? }else{
? ? ? ? _isAllSelected = YES;
? ? ? ? [sendersetTitle:@"全選"];
? ? ? ? for(NSIndexPath*indexPathinarr) {
? ? ? ? ? ? //根據(jù)索引胯盯,獲取cell 然后就可以做你想做的事情啦
? ? ? ? ? ? UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
? ? ? ? ? ? cell.accessoryType = UITableViewCellAccessoryCheckmark; //切換為選中
? ? ? ? }
? ? ? ? for(inti =0; i
? ? ? ? ? ? [self.selectedArrMaddObject:self.dataArray[i]];
? ? ? ? ? ? NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
? ? ? ? ? ? [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
? ? ? ? }
? ? }
? ? NSLog(@"=====%@", self.selectedArrM);
}
#pragma mark <UITableViewDelegate>
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
?? ? NSArray*subviews = [[tableViewcellForRowAtIndexPath:indexPath]subviews];
? ? ? ? NSString* name =self.dataArray[indexPath.row];
? ? UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
? ? if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { //如果為選中狀態(tài)
? ? ? ? cell.accessoryType = UITableViewCellAccessoryNone; //切換為未選中
? ? ? ? [self.selectedArrMremoveObject:name];
? ? ? ? [_selectIndexsremoveObject:indexPath];//數(shù)據(jù)移除
? ? }else{//未選中
? ? ? ? cell.accessoryType = UITableViewCellAccessoryCheckmark; //切換為選中
? ? ? ? [self.selectedArrMaddObject:name];
? ? ? ? [_selectIndexsaddObject:indexPath];//添加索引數(shù)據(jù)到數(shù)組
? ? }
? ? NSLog(@"0000====%@",self.selectedArrM);
}
- (NSMutableArray*)selectIndexs{
? ? if (_selectIndexs == nil) {
? ? ? ? _selectIndexs = [NSMutableArray array];
? ? }
? ? return _selectIndexs;
}
@end