UITableView多選刪除

整體代碼如下:

注:具體分的步驟只是根據(jù)demo來的

1、控件的初始化:(不用多說,都會)

allowsMultipleSelectionDuringEditing設置為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的指定位置
設置某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;
    }
默認對勾的顏色是藍色,如果想修改選中對勾的顏色 需要設置cell的tintColor 如下:

cell.tintColor = [UIColor redColor];

選中與未選中的效果圖如下:

![
![Simulator Screen Shot 2016年9月12日 下午6.03.38.png](http://upload-images.jianshu.io/upload_images/1787970-ac5066bcf86a1df9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
](http://upload-images.jianshu.io/upload_images/1787970-d8b92cdfa6fb9c73.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


至于全選刪除的功能代碼就需要自己研究邏輯了~
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容