ios - UITableViewCell 文本在 DispatchQueue 上缩小

标签 ios swift uitableview

我的 TableViewCell 中有一个标签。每当我使用 DispatchQueue 使单元格出列时,单元格中的标签就会缩小。相反,当我简单地将它出队时,标签变为正常。 可能的原因和修复方法是什么?

在调度队列之前 enter image description here

在 DispatchQueue 之后 enter image description here

代码:

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/

相关文章:

ios - 避免在自定义 UIViewController 容器中加载数百个 View Controller

objective-c - 更改 UISegmentedControl selectedSegmentIndex 不会更改所选索引

ios - UITableView自定义删除按钮功能

iOS - 方法正在处理时禁用行选择

ios - 如何在 UITableview 单元格中正确显示分隔的 JSON 数据?

iOS - AVFoundation 是否支持 3D 条形码?

ios - 如何在 swift 中使用 dbAccess 的特定字段更新多条记录

ios - 将模态视图放在 View 上并使背景变灰

iOS 应用程序 : How to access S3 object saved under cognito identity id folder by another user

iphone - 自定义 UIITableViewCell 中的 UIImageView 在重新加载时不会切换