【问题标题】:UITableView - can't select cells, cells overlapUITableView - 无法选择单元格,单元格重叠
【发布时间】:2014-10-22 21:47:29
【问题描述】:

非常简单的表格视图:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SettingsCell"];
    UILabel *label = (UILabel *)[cell viewWithTag:1];

    switch (indexPath.row) {
        case 0:
        {
            label.text = @"One";
            break;
        }
        case 1:
        {
            label.text = @"Two";
            break;
        }
        default:
            break;
    }

    return cell;
}

当我点击一个单元格时不会调用didSelectRowAtIndexPath。

单元格也重叠:

这里会发生什么?我可以滚动表格,但无法选择单元格。在某一时刻,这是有效的,我不知道出了什么问题。

我正在使用原型单元格,并且在情节提要中将“选择”设置为“单选”。所有视图都有 userInteractionEnabled = YES。

【问题讨论】:

    标签: ios xcode uitableview didselectrowatindexpath


    【解决方案1】:

    您似乎在这里重复引用了第一个单元格:

    UILabel *label = (UILabel *)[cell viewWithTag:1];

    这可能导致文本被覆盖。

    尝试类似:

    if(indexPath.row == 1)
    {
        cell.textLabel.text = @"One";
    }
    

    【讨论】:

      【解决方案2】:

      这是解决方案:

      - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
          return 44;
      }
      

      将返回类型从“float”更改为“CGFloat”。 Xcode 6.1 甚至没有将此标记为警告。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-09
        • 1970-01-01
        • 1970-01-01
        • 2011-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多