【问题标题】:UITableView self sizing cell scrolling issues with big difference of cell heightUITableView 自调整单元格滚动问题,单元格高度差异很大
【发布时间】:2016-05-20 09:50:50
【问题描述】:
  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

    let json = JSON(data: activityTableView.onlineFeedsData)[indexPath.row] // new
    if(json["topic"]["reply_count"].int > 0){
        if let cell: FeedQuestionAnswerTableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier_reply) as? FeedQuestionAnswerTableViewCell{

            cell.selectionStyle = .None
            cell.tag = indexPath.row
            cell.relatedTableView = self.activityTableView
            cell.configureFeedWithReplyCell(cell, indexPath: indexPath, rect: view.frame,key: "activityTableView")
            // Make sure the constraints have been added to this cell, since it may have just been created from scratch
            cell.layer.shouldRasterize = true
            cell.layer.rasterizationScale = UIScreen.mainScreen().scale

            return cell
        }
    }else{
        if let cell: FeedQuestionTableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as? FeedQuestionTableViewCell{

            cell.selectionStyle = .None
            cell.tag = indexPath.row
            cell.relatedTableView = self.activityTableView
            cell.configureFeedCell(cell, indexPath: indexPath, rect: view.frame)
            // Make sure the constraints have been added to this cell, since it may have just been created from scratch

            cell.layer.shouldRasterize = true
            cell.layer.rasterizationScale = UIScreen.mainScreen().scale
            return cell
        }
    }


    return UITableViewCell()
}

每个单元格的高度有200-400的差异,所以尝试在estimatedHeightForRowAtIndexPath中实现高度计算。我不能用这种方法精确估计高度作为计算的bcz

并在willDisplayCell 方法中缓存高度,但滚动仍然会跳跃,就像渲染需要时间一样。

该单元格类似于 Facebook 卡片式布局,其中包含大量动态数据以及可变高度的文本和图像。有人可以帮助我吗?谢谢

【问题讨论】:

  • 你能告诉我更多关于如何计算tableView(_:estimatedHeightForRowAtIndexPath:)中单元格的高度吗?

标签: ios swift uitableview ios8 uitableviewautomaticdimension


【解决方案1】:

如果您能够计算每个单元格的高度,只需使用 tableView(_:heightForRowAtIndexPath) 而不是 estimatedRowHeightAtIndexPath 属性。 estimatedRowHeightAtIndexPath 主要用于 smthg,如自调整大小的单元格等。

【讨论】:

  • NSMutableDictionary *cellHeightsDictionary; // 在代码中初始化 cellHeightsDictionary = @{}.mutableCopy;
【解决方案2】:

试试这个。我希望这可以帮助你。它对我有用。实际上,这会在显示之前计算单元格高度。 谢谢。

NSMutableDictionary *cellHeightsDictionary;

// 把这个放到viewDidLoad中

cellHeightsDictionary = @{}.mutableCopy;

// UITableview 委托方法

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
NS_AVAILABLE_IOS(7_0)
{
    NSNumber *height = [cellHeightsDictionary objectForKey:indexPath];
    if (height) return height.doubleValue;
    return UITableViewAutomaticDimension;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cellHeightsDictionary setObject:@(cell.frame.size.height) forKey:indexPath];

}

【讨论】:

  • thnks @yogesh 我肯定会尝试,但我最终删除了自动布局并使用 AsyncdisplayKit 在框架中对其进行编码以实现 ma 要求
猜你喜欢
  • 2019-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-10
  • 2012-07-22
  • 2016-08-21
相关资源
最近更新 更多