oc tableview多选

  • 代理
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    - (UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath {
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
    }



    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
    }

    //取消选择cell
    -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    // UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    CommodityConfigModel *model = self.dataArray[indexPath.row];
    [self.selectArray addObject:model.commodity_id];
    self.isAllSelect = NO;
    }
    //选择cell
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    CommodityConfigModel *model = self.dataArray[indexPath.row];
    [self.selectArray addObject:model.commodity_id];
    }
  • 加判断
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    - (void)allSelectAction {

    self.isAllSelect = 1-self.isAllSelect;
    if (_isAllSelect) {
    for (int i = 0; i < self.dataArray.count; i ++) {
    CommodityConfigModel *model = self.dataArray[i];
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
    [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
    [self.selectArray addObject:model.commodity_id];
    }
    }else {
    for (int i = 0; i < self.dataArray.count; i ++) {
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    [self.selectArray removeAllObjects];
    }
    }