【问题标题】:Issue While Finding Height of UITableViewCell查找 UITableViewCell 高度时的问题
【发布时间】:2016-08-05 08:52:22
【问题描述】:

我正在heightForRowAtIndexPath- 中以编程方式计算单元格的高度

我的完整方法是:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id cell = [self.tableView dequeueReusableCellWithIdentifier:kCellID];

    if([cell isKindOfClass:[Custom class]])
    {
        return 100;
    }
    else
    {
         return 20;
    }
}

我的应用需要 3-5 秒来显示 ViewController。虽然cellForRowAtIndexPath 被调用并且我可以在控制台中显示日志。

但我的 ViewController 没有加载。

当使用此代码应用程序返回高度时工作正常:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 100;
}

我不知道这是这条线的问题:

id cell = [self.tableView dequeueReusableCellWithIdentifier:kCellID];

我在这个问题中缺少什么?

【问题讨论】:

  • @Anbu.Karthik 我需要找到单元格的类型,因为我有两种类型的单元格。这是主要问题。每当我使用代码查找单元格类型时,我的应用就会开始延迟
  • 为什么不使用属性 isTableViewWithCustomCells 或 isTableViewWithOtherCustomCells?它应该更快。我认为heightForRowAtIndexPath: 可能会在 cellForRowAtIndexPath 和出列的东西之前被调用。
  • 有时heightForRowAtIndexPath 在 cellForRowAtIndexPath 之前调用然后呢?

标签: ios objective-c uitableview ios8


【解决方案1】:

cellForRowAtIndexPath: 方法中有一些逻辑可以确定该行应该是什么类型的单元格。将该逻辑添加到 heightForRowAtIndexPath: 方法中以确定每行的高度应该是多少。

【讨论】:

    【解决方案2】:

    你的这行写错了

    id cell = [self.tableView dequeueReusableCellWithIdentifier:kCellID];
    

    它会强制你的细胞重复使用。

    使用

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        id cell=[tableView cellForRowAtIndexPath:indexPath];
    
        if([cell isKindOfClass:[CustomCellClass class]])
        {
            return 100;
        }
        else
        {
             return 20;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 1970-01-01
      相关资源
      最近更新 更多