ios - XCode 6 问题 - dequeueReusableCellWithIdentifier 不返回自定义单元格高度

标签 ios objective-c uitableview heightforrowatindexpath

我有一个 UITableViewController,其中包含 2 个具有自定义高度的原型(prototype)单元格 - 一个高度为 187 点,另一个高度为 140 点。 tableview 的默认行高为 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。事实上,当我查看 Storyboard(作为文本)时,没有任何地方提到 187。但是 XCode 似乎记住了之前的值。

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

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

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

最佳答案

heightForRowAtIndexPathcellForRowAtIndexPath 之前被调用,因此您永远不能使用它来尝试引用单元格并获取高度。使用以下日志行和结果查看以下代码:

- (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 处应该存在什么类型的单元格,因此单元格应该有多高.

关于ios - XCode 6 问题 - dequeueReusableCellWithIdentifier 不返回自定义单元格高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25995932/

相关文章:

ios 将第一个字符本地化为部分名称

ios - objc_setAssociatedObject 函数错误在 64 位模式下不在 32 位

ios - 如何处理此设计 - iOS

objective-c - IBOutlet 声明?

objective-c - 回调函数在提取双指针时存在问题

ios - 我可以查明单元格何时排队吗?

ios - Firebase 崩溃报告 : Failed to transmit crash: Error 400

ios - 我必须将什么拖到哪里才能通过点击背景关闭键盘?

ios - 什么是 Parse Table View 默认排序方法?

ios - UITableview中Multiple cells的数组结构如何设计?