先看下效果圖
直接上代碼
#import "MyController.h" @interface MyController () { UIButton *button; } @property(nonatomic,strong)NSMutableArray *array;//數據源 @property (nonatomic,strong)NSMutableArray *selectorPatnArray;//存放選中數據 @end @implementation MyController - (void)viewDidLoad { [super viewDidLoad]; //添加數據源 for (int i = 0; i < 10; i++) { NSString *str = [NSString stringWithFormat:@"第%d行",i + 1]; [self.array addObject:str]; } button = [UIButton buttonWithType:(UIButtonTypeCustom)]; [button setTitle:@"選擇" forState:(UIControlStateNormal)]; [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)]; button.frame = CGRectMake(0, 0, 50, 20); [button addTarget:self action:@selector(selectMore:) forControlEvents:(UIControlEventTouchUpInside)]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.array.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *Identifier = @"myCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier]; } cell.textLabel.text = self.array[indexPath.row];
cell的selectionStyle不要設置為UITableViewSelectionStyleNone
return cell; } - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //選中數據 [self.selectorPatnArray addObject:self.array[indexPath.row]]; } - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{ //從選中中取消 if (self.selectorPatnArray.count > 0) { [self.selectorPatnArray removeObject:self.array[indexPath.row]]; } } #pragma mark - 點擊事件 - (void)selectMore:(UIBarButtonItem *)action{ if ([button.titleLabel.text isEqualToString:@"選擇"]) { //移除之前選中的內容 if (self.selectorPatnArray.count > 0) { [self.selectorPatnArray removeAllObjects]; } [button setTitle:@"確認" forState:(UIControlStateNormal)]; //進入編輯狀態 [self.tableView setEditing:YES animated:YES]; }else{ [button setTitle:@"選擇" forState:(UIControlStateNormal)];
//對選中內容進行操作 NSLog(@"選中個數是 : %lu 內容為 : %@",(unsigned long)self.selectorPatnArray.count,self.selectorPatnArray); //取消編輯狀態 [self.tableView setEditing:NO animated:YES]; } } #pragma mark -懶加載 -(NSMutableArray *)array{ if (!_array) { _array = [NSMutableArray array]; } return _array; } - (NSMutableArray *)selectorPatnArray{ if (!_selectorPatnArray) { _selectorPatnArray = [NSMutableArray array]; } return _selectorPatnArray; }
如果要把tableView在非編輯狀態下不讓點擊,設置下這個屬性,就OK了.
@property (nonatomic) BOOL allowsSelection NS_AVAILABLE_IOS(3_0); // default is YES. Controls whether rows can be selected when not in editing mode
如果在某些情況下需要全選,可以按照這個思路:
for (int i = 0; i < self.array.count; i++) { NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0]; UITableViewCell *cell = (UITableViewCell *)[self.tableView cellForRowAtIndexPath:path]; cell.selected = YES; [self.selectorPatnArray addObject:self.array[i]];//添加到選中列表 }
這只是個人想法,歡迎指出不足......
