我创建了一个Swift UITableView帖子,每个帖子都包含一些文字和图表图片。使用SDWebImage和Firebase异步加载图像。图像具有不同的高度,但具有固定的宽度。
以下是显示显示问题的简短视频:https://youtu.be/QzQFT2z0GjA
有些单元格第一次显示不正确,但在滚动后看起来很完美。我读到了使用layoutIfNeeded和setNeedsLayout as suggested in this post或iOS 11 UITableViewAutomaticDimension,但它似乎不适用于我的情况。
这是我的代码:
var postArray : [Post] = [Post]()
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[indexPath.row].title
cell.postSource.text = postArray[indexPath.row].source
cell.postChart.sd_setImage(with: URL(string: postArray[indexPath.row].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()
})
}
有什么想法吗?提前谢谢。
答案 0 :(得分:0)
cell.layoutIfNeeded()
这应该在
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
答案 1 :(得分:0)
解决方案包括在Post对象中添加chartWidth和chartHeight参数,并为Firebase数据库中的每个帖子添加它们的值,然后设置一些约束以在下载图像之前预先计算单元格高度。
在TableViewCell.swift中添加:
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)
}
在ViewController中使用setChartSize函数:
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[indexPath.row]
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
}
在下载图表后调整单元格大小等任何其他选项都会生成滚动跳转。
答案 2 :(得分:0)
迅速5+ iOS 13 Xcode 11.2.1以上 惊人的答案 https://stackoverflow.com/a/58832555/6881070
override func viewDidAppear(_ animated: Bool) {
// self.tablev.frame.origin.y = vnavigation.frame.maxY + 5
// tablevheight = self.tablev.frame.size.height
self.bgview.backgroundColor = UIColor.init(patternImage: UIImage(named: "chat_bg.png")!)
self.registerForKeyboardNotifications()
UIView.performWithoutAnimation {
tablev.beginUpdates()
tablev.endUpdates()
}
}