ios - 滚动后 UITableView 单元格正确
<p><p>我创建了一个 Swift UITableView 帖子,每个帖子都包含一些文本和图表图像。使用 SDWebImage 和 Firebase 异步加载图像。图片有不同的高度,但宽度是固定的。</p>
<p>这是一个显示显示问题的短视频:<a href="https://youtu.be/QzQFT2z0GjA" rel="noreferrer noopener nofollow">https://youtu.be/QzQFT2z0GjA</a> </p>
<p>有些单元格第一次显示不正确,但滚动后看起来很完美。我读到了关于使用 layoutIfNeeded 和 setNeedsLayout <a href="https://stackoverflow.com/questions/25971147/uitableview-dynamic-cell-heights-only-correct-after-some-scrolling" rel="noreferrer noopener nofollow">as suggested in this post</a>或 iOS 11 UITableViewAutomaticDimension 但在我的情况下似乎不起作用。</p>
<p>这是我的代码:</p>
<pre><code>var postArray : = ()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
postTableView.delegate = self
postTableView.dataSource = self
aLaUneWidthConstraint.constant = view.frame.size.width/2
etatFranceWidthConstraint.constant = view.frame.size.width/2
postTableView.register(UINib(nibName:"TableViewCell", bundle: nil), forCellReuseIdentifier: "postCell")
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
activityIndicator.startAnimating()
retrievePosts()
}
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.isNavigationBarHidden = true
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! TableViewCell
tableView.separatorStyle = UITableViewCellSeparatorStyle.none
cell.selectionStyle = UITableViewCellSelectionStyle.none
cell.postTitle.text = postArray.title
cell.postSource.text = postArray.source
cell.postChart.sd_setImage(with: URL(string: postArray.chartURL!), placeholderImage: UIImage(named: "placeholder.png")) { (image, error, cache, url) in
cell.chartHeightConstraint.constant = ((cell.postChart.image?.size.height)!/2)
cell.setNeedsLayout()
cell.layoutIfNeeded()
}
return cell
}
func retrievePosts() {
let postDB = Database.database().reference().child("Posts")
postDB.observe(.childAdded, with: { (snapshot) in
let snapshotValue = snapshot.value as! NSDictionary
let title = snapshotValue["title"] as! String
let source = snapshotValue["source"] as! String
let chartURL = snapshotValue["chartURL"] as! String
let category = snapshotValue["category"] as! String
let post = Post(data: snapshotValue)
post.title = title
post.source = source
post.chartURL = chartURL
post.category = category
self.postArray.append(post)
self.activityIndicator.stopAnimating()
self.activityView.isHidden = true
self.activityView.frame.size.height = 0
self.postTableView.reloadData()
})
}
</code></pre>
<p>有什么想法吗?提前致谢。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>解决方案包括在 Post 对象中添加 chartWidth 和 chartHeight 参数,并为 Firebase 数据库中的每个帖子添加它们的值,然后设置一些约束以在下载图像之前预先计算单元格高度。</p>
<p>在 TableViewCell.swift 中添加:</p>
<pre><code>func setChartSize(size: CGSize) {
postChart.removeConstraint(postChartRatioConstraint)
postChartRatioConstraint = NSLayoutConstraint(
item: postChart,
attribute: .height,
relatedBy: .equal,
toItem: postChart,
attribute: .width,
multiplier: (size.height / size.width),
constant: 0)
postChart.addConstraint(postChartRatioConstraint)
}
</code></pre>
<p>在 ViewController 中使用 setChartSize 函数:</p>
<pre><code>func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! TableViewCell
tableView.separatorStyle = UITableViewCellSeparatorStyle.none
cell.selectionStyle = UITableViewCellSelectionStyle.none
let post = postArray
cell.postTitle.text = post.title
cell.postSource.text = post.source
cell.postChart.sd_setShowActivityIndicatorView(true)
cell.postChart.sd_setIndicatorStyle(.white)
cell.setChartSize(size: CGSize(width: post.chartWidth!, height: post.chartHeight!))
cell.postChart.sd_setImage(
with: URL(string: post.chartURL!),
placeholderImage: UIImage(named: "placeholder.png"),
options: [.continueInBackground],
completed: nil)
return cell
}
</code></pre>
<p>下载图表后调整单元格大小等任何其他选项都会生成滚动跳转。</p></p>
<p style="font-size: 20px;">关于ios - 滚动后 UITableView 单元格正确,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/48057813/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/48057813/
</a>
</p>
页:
[1]