1

我有一个 UITableViewController 有 2 个具有自定义高度的原型单元格 - 一个高度为 187 点,另一个高度为 140 点。tableview 的默认行高是 ser to 187。

我的 cellForRowAtIndexPath 看起来像这样:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if (indexPath.section == 0) {
        MyListingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyListingCell"];
        [cell initWithListing:[self.listings objectAtIndex:indexPath.row]];
        return cell;
    } else {
        return [tableView dequeueReusableCellWithIdentifier:@"AddMyListingCell"];
    }
}

我有一个匹配的 heightForRowAtIndexPath ,如下所示:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyListingCell"];
        return cell.frame.size.height;
    } else {

        //if no listings exist, make the cell full-screen height
        if (self.listings.count == 0) {   
            return tableView.frame.size.height;
        } else {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddMyListingCell"];
            return cell.frame.size.height;
        }
    }
}

当我运行代码时,单元格高度正确显示。分别为 187 和 140。

但是,我想将较大单元格的大小更改为 213 点。为此,我将自定义表格单元格的高度更改为 213。问题是当我运行代码(使用 XCode 6)时,表格单元格的高度仍然为 187。

我尝试将默认的 tableview rowheight 属性也更改为 213。但是我仍然得到 187。事实上,当我查看情节提要(作为文本)时,任何地方都没有提到 187。但似乎 XCode 正在记住以前的值。

我尝试过清理、深度清理、重新启动 xcode、从手机中删除应用程序以及删除我的调试产品。我无法忘记187。

这可能是 XCode 6 中的错误吗? 在 XCode 5 中,这不会发生(已证明)。自定义单元格高度在 XCode 5 中运行良好,但在我安装 XCode 6 后的第二天就出现了这个问题。

任何人都可以就尝试的事情提供一些建议,或者确认我不会发疯。

4

1 回答 1

1

heightForRowAtIndexPath被称为 BEFORE cellForRowAtIndexPath,因此您永远不能使用它来尝试引用单元格并获取高度。查看以下代码以及以下日志行和结果:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"CELL");
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"HEIGHT");
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSLog(@"%@", cell);
    return cell.frame.size.height;
}

这导致:

2014-09-23 09:55:05.114 NewTesting[54040:60b] HEIGHT
2014-09-23 09:55:05.114 NewTesting[54040:60b] (null)
2014-09-23 09:55:05.115 NewTesting[54040:60b] HEIGHT
2014-09-23 09:55:05.115 NewTesting[54040:60b] (null)
2014-09-23 09:55:05.115 NewTesting[54040:60b] HEIGHT
2014-09-23 09:55:05.116 NewTesting[54040:60b] (null)
2014-09-23 09:55:05.116 NewTesting[54040:60b] HEIGHT
2014-09-23 09:55:05.116 NewTesting[54040:60b] (null)
2014-09-23 09:55:05.117 NewTesting[54040:60b] CELL
2014-09-23 09:55:05.118 NewTesting[54040:60b] CELL
2014-09-23 09:55:05.119 NewTesting[54040:60b] CELL
2014-09-23 09:55:05.120 NewTesting[54040:60b] CELL

考虑到这一点,您使用的方法永远不会奏效。

您是否尝试过更简单的方法,如下所示(尽管我个人会使用常量)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath.section == 0 ? 213 : 140;
}

这是假设第一部分中的所有单元格都是 213 高,而任何其他部分都是 140 高,这可能是不正确的。

最终,我认为做你想做的最好的方法是询问你的数据源在给定的情况下应该存在什么类型的单元格indexPath,因此单元格应该有多高。

于 2014-09-23T14:03:53.880 回答