8
  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.

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

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

4

2 回答 2

0

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

于 2016-03-06T19:04:41.317 回答
0

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

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];

}
于 2017-01-12T07:04:19.347 回答