【问题标题】:UITableView selection -- multiple selectionUITableView 选择——多选
【发布时间】:2014-04-20 10:57:40
【问题描述】:

我有一个奇怪的 uitableview 选择问题。尝试在预选几个单元格的情况下使用多重选择。多选设置为真。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.textLabel.font = [UIFont systemFontOfSize: 15.0f];
    cell.textLabel.text = [data objectAtIndex:indexPath.row];

    //    if([_selectedInts containsObject:[NSNumber numberWithInt:(int)indexPath.row]]){
    //
    //        [cell setSelected:YES animated:NO];
    //
    //    } else {
    //
    //        [cell setSelected:NO animated:NO];
    //
    //    }


    return cell;
}



-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    [cell setUserInteractionEnabled:YES];


    if([_selectedInts containsObject:[NSNumber numberWithInt:(int)indexPath.row]]){

        [cell setSelected:YES animated:YES];
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    } else {

        [cell setSelected:NO animated:YES];
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }

}

当 tableview 加载时,由于一个整数列表标记了需要选择的索引,因此选择了一堆单元格。

这些单元格停止响应,并且在被点击时不会发出 didselect 和 diddeselect 方法。

有什么问题?

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    我不确定为什么将代码放在 willDisplayCell 中会导致这种情况发生,但我在尝试您的代码时发现了同样的情况。将代码放入 cellForRowAtIndexPath: 如果我使用 selectRowAtIndexPath:animated:scrollPosition: 而不是 setSelected: 则有效(如果我使用 setSelected:,单元格不会显示选择颜色,但它们是响应式的)。因此,此代码用于显示复选标记和选择颜色,并在选择或取消选择其他单元格时正确更改了该状态。另请注意,我将单元格出列,因为这比您创建单元格的方式更节省内存,

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        cell.textLabel.font = [UIFont systemFontOfSize: 15.0f];
        cell.textLabel.text = [data objectAtIndex:indexPath.row];
    
        if([_selectedInts containsObject:@(indexPath.row)]){
            [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
            [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
        } else {
            [cell setAccessoryType:UITableViewCellAccessoryNone];
        }
        return cell;
    }
    
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        [self.selectedInts addObject:@(indexPath.row)];
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }
    
    
    -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
        [self.selectedInts removeObject:@(indexPath.row)];
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }
    

    【讨论】:

      【解决方案2】:

      在 cellForRowAtIndexPath 方法中,是否真的需要在单元格可见时创建新单元格?

      我们试试看,这是苹果推荐的最佳做法:

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
      static NSString *CellIdentifier = @"Cell";
      UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
      }
      cell.textLabel.text = @"a";
      return cell;
      }
      

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多