我的 TableViewCell 中有一个标签。每当我使用 DispatchQueue 使单元格出列时,单元格中的标签就会缩小。相反,当我简单地将它出队时,标签变为正常。 可能的原因和修复方法是什么?
代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let post = posts[indexPath.row]
if let cell = tableView.dequeueReusableCell(withIdentifier: "FeedCell") as? FeedCell {
DispatchQueue.main.async {
cell.configureCell(post: post)
}
return cell
} else {
return FeedCell()
}
}
FeedCell(UITableViewCell) 中标签布局的代码:
override func layoutSubviews() {
super.layoutSubviews()
contentView.layoutIfNeeded()
self.caption.sizeToFit()
}
contentView
是 Label 所在的 UIView。
最佳答案
不需要将该函数分派(dispatch)到主队列,因为 cellForRowAt:
将始终在主队列上调用。
由于您正在异步调度 configureCell
调用,您最终会从 cellForRowAt:
返回一个未配置的单元格,这会导致 self.caption.sizeToFit()
为空标题调用(或者如果单元格随后被重复使用,它将根据该单元格对象中之前的任何标题调整大小)。
您可以确保 configureCell
触发单元格重新布局,但删除不必要的 DispatchQueue.main.async
关于ios - UITableViewCell 文本在 DispatchQueue 上缩小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47004566/