整體代碼如下:
注:具體分的步驟只是根據(jù)demo來的
1呛梆、控件的初始化:(不用多說凯傲,都會)
allowsMultipleSelectionDuringEditing設(shè)置為YES tableview允許多選
- (void)viewDidLoad {
[super viewDidLoad];
self.testTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, M_SIZE.width, M_SIZE.height) style:UITableViewStyleGrouped];
self.testTableView.delegate = self;
self.testTableView.dataSource = self;
[self.view addSubview:self.testTableView];
textArry = @[@"one", @"two", @"three", @"four", @"five"];
// 控制tableview在可編輯狀態(tài)下 - 可多選
self.testTableView.allowsMultipleSelectionDuringEditing = YES;
// 添加一個可以控制tableview編輯狀態(tài)的按鈕
UIButton *buttonEdit = [UIButton buttonWithType:UIButtonTypeCustom];
buttonEdit.frame = CGRectMake(M_SIZE.width - 40, M_SIZE.height - 40, 20, 20);
[buttonEdit setBackgroundColor:[UIColor orangeColor]];
[buttonEdit addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonEdit];
}
2犬辰、button的點擊事件:
[self.testTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];```
indexPath:指定選中cell所在的行和列
UITableViewScrollPosition:枚舉類型 將選中的cell移動到tableview的指定位置
設(shè)置某cell未選中:
[self.testTableView deselectRowAtIndexPath:indexPath animated:YES];
- (void)buttonAction:(UIButton *)sender {
self.testTableView.editing = !self.testTableView.editing;
// 簡單實現(xiàn)以下全選的效果 :
for (int i = 0; i < textArry.count; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
if (self.testTableView.editing) {
[self.testTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
// UITableViewScrollPosition 選中行滾動tableview的哪個位置
}else{
[self.testTableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
}
######3、代理方法:(純屬廢話)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return textArry.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.tintColor = [UIColor redColor];
cell.textLabel.text = textArry[indexPath.row];
return cell;
}
默認(rèn)對勾的顏色是藍色冰单,如果想修改選中對勾的顏色 需要設(shè)置cell的tintColor 如下:
cell.tintColor = [UIColor redColor];
選中與未選中的效果圖如下:

](http://upload-images.jianshu.io/upload_images/1787970-d8b92cdfa6fb9c73.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
至于全選刪除的功能代碼就需要自己研究邏輯了~