我正在尝试建立一个类似 Facebook 的提要。我正在使用在情节提要中设置了自动布局约束的标准 Tableview。
宽度应为视口的 100%,高度取决于图像的纵横比。
我正在使用 Haneke 的 hnk_setImageFromURL 从服务器加载异步图像。
我在这里看到了使用tableview.beginUpdates()
或的选项endUpdates()
,它导致第一次渲染工作正常,但是一旦我开始滚动高度计算出错,单元格显示为空白和/或应用程序由于 indexOutOfBounds 而崩溃。
heightForRowAtIndexPath
使用服务器预先给定的纵横比手动计算高度是唯一的选择吗?
我的相关代码部分
ViewController:
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 500
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("FeedItem", forIndexPath: indexPath) as! FeedItemTableViewCell
content.hnk_setImageFromURL(NSURL(string: contentImage)!, placeholder: nil, format: nil, failure: nil, success: {image in
cell.content.image = image
self.beginUpdates()
self.endUpdates()
}
)
}
PS 使用 Haneke 引起的副作用是我必须为 UIImageView 提供一些初始大小,目前我正在使用占位符图像,当从服务器加载图像时会导致恼人的动画