菜鸟教程小白 发表于 2022-12-11 19:19:52

ios - UITableViewCells 在 X 个单元格后停止正确显示


                                            <p><p>Swift 4 + Xcode 9。</p>

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

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

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

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

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

<pre><code>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 &amp;&amp; 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
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>对于遇到这个问题的任何人,由于完全没有解决这个问题,我得出的结论是 UIKit 不适合我的目的。我选择了<a href="http://texturegroup.org" rel="noreferrer noopener nofollow">Texture</a> (以前的 AsyncDisplayKit),我的进度已经恢复。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UITableViewCells 在 X 个单元格后停止正确显示,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/47366215/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/47366215/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UITableViewCells 在 X 个单元格后停止正确显示