OGeek|极客世界-中国程序员成长平台

标题: ios - UITableViewCells 在 X 个单元格后停止正确显示 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:19
标题: ios - UITableViewCells 在 X 个单元格后停止正确显示

Swift 4 + Xcode 9。

我已经解决这个问题好几个星期了,试图自己解决它。我将不胜感激任何人可以提供的任何帮助。我没有在我最初的问题中发布任何代码,因为它是专有的。如果需要帮助解决我的问题,我很乐意提供其中的一部分。

我有一个带有自定义单元格的 UITableView,其中包含非常复杂的 subview 布局,其中一些被隐藏或显示(使用 1000-priority cellForRowAt) 期间添加和删除的 height=0 约束取决于数据。还有一个 ImageView,它应该始终是全宽的,并且应该改变高度以匹配通过 Kingfisher.shared.retrieveImage() 加载的图像。获得图像后,我会更新该单元格的图像上的 aspectRatio 约束,然后显示单元格。这对于前 15-20 个单元格非常有效,但是当我滚动浏览更多行时,它就会停止运行。图像很小且居中,某些数据字段未更新等。如果我继续滚动,有时单元格会在这里和那里正常运行,但几乎所有单元格都不会。

现在是有趣的部分:如果我向上滚动,每个单元格都会自动重新格式化以使其看起来像应有的样子,之后,每个单元格都是完美的。代码显然有效 - 我觉得这可能是平台中的一个错误,但在我假设之前,我想看看其他人是否遇到过这样的事情。

再次,非常感谢您提供的任何帮助 - 我非常渴望解决这个问题。

更新:为了回答几个问题,这里有一段代码,它是自定义单元类的一部分。这就是我设置单元格图像的方式(包括更新纵横比约束),以及我如何重置单元格以供重复使用。

internal var aspectConstraint : NSLayoutConstraint? {
    didSet {
        if oldValue != nil {
            imageView.removeConstraint(oldValue!)
        }
        if aspectConstraint != nil {
            imageView.addConstraint(aspectConstraint!)
        }
    }
}

override func prepareForReuse() {
    super.prepareForReuse()
    aspectConstraint = nil
    imageView.image = nil
    for view in subviews {
        for c in view.constraints {
            if c.firstAttribute == NSLayoutAttribute.height && c.constant == 0 {
                view.removeConstraint(c)
            }
        }
    }
}

func setCustomImage(image : UIImage) {
    let aspect = image.size.width / image.size.height
    let constraint = NSLayoutConstraint(item: imageView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: imageView, attribute: NSLayoutAttribute.height, multiplier: aspect, constant: 0.0)
    constraint.priority = UILayoutPriority(999)
    aspectConstraint = constraint
    imageView.image = image
}



Best Answer-推荐答案


对于遇到这个问题的任何人,由于完全没有解决这个问题,我得出的结论是 UIKit 不适合我的目的。我选择了Texture (以前的 AsyncDisplayKit),我的进度已经恢复。

关于ios - UITableViewCells 在 X 个单元格后停止正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47366215/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4