带有动态高度错误的异步图片设置为单元格 [英] Async image set to cell with dynamic height bug
问题描述
当我尝试下载图像并将其异步设置为具有动态高度的单元格时,我的应用程序遇到了错误。
I'm experiencing bugs with my application when I'm trying to download and set an image async to a cell with dynamic height.
该错误的视频: https://youtu.be/nyfjCmc0_Yk
我一无所知:不明白为什么会这样。我保存了单元格的高度,以防止出现跳动问题和东西,甚至在设置图像后甚至更新了单元格的高度。
I'm clueless: can't understand why it happens. I'm saving the cell heights for preventing jumping issues and stuff, I do even update the height of the cell after setting the image.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var post : Post
var cell : PostCell
post = Posts.shared.post(indexPath: indexPath)
// I REMOVED SOME NOT IMPORTANT PARTS OF THE CODE
// (like setting the text, etc)
if post.images.count > 0 {
// Images is attached to the post
cell.postImageView.sd_setImage(with: URL(string: appSettings.url + "/resources/img/posts/" + post.images[0]), placeholderImage: nil, options: [.avoidAutoSetImage]) { (image, error, type, url) in
if let error = error {
// placeholder
return
}
DispatchQueue.main.async {
let cgRect = image!.contentClippingRect(maxWidth: 300, maxHeight: 400)
cell.postImageView.isHidden = false
cell.postImageWidthConstraint.constant = cgRect.width
cell.postImageViewHeightConstraint.constant = cgRect.height
cell.postImageView.image = image
cell.layoutIfNeeded()
self.cellHeights[indexPath] = cell.frame.size.height
}
}
} else {
// No image is attached to the post
cell.postImageViewHeightConstraint.constant = 0
cell.postImageWidthConstraint.constant = 0
cell.postImageView.isHidden = true
}
return cell
}
var cellHeights: [IndexPath : CGFloat] = [:]
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cellHeights[indexPath] = cell.frame.size.height
cell.layoutIfNeeded()
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
if cellHeights[indexPath] != nil {
return CGFloat(Float(cellHeights[indexPath] ?? 0.0))
}
else {
return UITableViewAutomaticDimension
}
}
我尝试致电 tableView.beginUpdates()
tableView.endUpdates()
,它从一开始就解决了该问题,但是在滚动时会产生一个奇怪的错误:
I've tried calling tableView.beginUpdates()
tableView.endUpdates()
, it fixes the problem at the beginning, but when scrolling it creates a weird bug :
(在表格视图中随机出现在图像的一小部分)
(random appearing small part of the image in the tableview)
向上滚动时,它会跳转到开头的帖子..可能是由于高度值不正确吗?
And when scrolling up, it jumps to the beginning of the post.. maybe due to the incorrect height value?
我该怎么做?
推荐答案
如果要计算 UIImage
数据的高度和宽度并将这些值分配为,则肯定会出现此类错误。公司 UIImageView
的nstraint常数,尤其是在单元格中。
You'll surely get such kind of bug if you are to compute the height and the width of the UIImage
data and assign those values as the constraint constants of the UIImageView
especially in a cell.
那可以解决吗?使您的数据源/服务器具有计算出的宽度和高度,并避免自己计算。这意味着在每个 Post
对象中,应该有一个可用的宽度和高度值。这样速度更快,并解决了您的问题(请参阅我的这个可爱的个人快速项目: https://itunes.apple.com/us/app/catlitter-daily-dose-of-cats/id1366205944?ls=1&mt=8 )
Solution for that? Make your datasource/server have a computed width and height and avoid computing it yourself. Meaning in your each Post
object, there should be a ready width and height values. That's faster and that solved my issue the same as yours (see this cute personal quick project of mine: https://itunes.apple.com/us/app/catlitter-daily-dose-of-cats/id1366205944?ls=1&mt=8)
此外,我还从一些知名公司(如Facebook)提供的一些公共API中了解到(每个图像的计算尺寸都已准备好)。
Also, I've learned that from some public APIs provided by some established companies like Facebook (each images have its computed sizes ready).
我希望这会有所帮助。
这篇关于带有动态高度错误的异步图片设置为单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!