gpt4 book ai didi

ios - heightForRowAt 在 UITableView 中的 cellforRowAt 之后被调用

转载 作者:行者123 更新时间:2023-11-30 12:02:03 25 4
gpt4 key购买 nike

每当我重新加载 tableView 时,heightForRowAt 都会在 cellForRowAt 之后调用。它遵循的顺序是:

 numberofsections -> numberOfRowsInSection -> cellForRowAt -> heightForRow

我已经重写了 numberOfSections、numberOfRowsInSection、viewForHeaderInSection 方法。这些对 heightForRowcellForRowAt 的调用顺序有影响吗?

我使用的是 Xcode 9.1,在 iOS 11 上运行的 Swift 3。

最佳答案

这里有两个选择:

  1. 按照定义单元格大小的方式创建单元格的 contentView。这意味着您的垂直布局不应过于雄心勃勃。
  2. 手动计算其大小。总结 subview 之间的偏移量(高度)。这里可能是计算标签/ TextView 大小的问题(留下用于计算我使用的文本大小的辅助扩展)。注意textView.contentInsets!您也需要记住才能获得正确的尺寸

    extension String {
    func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
    let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
    let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin,
    attributes: [NSAttributedStringKey.font: font], context: nil)

    return ceil(boundingBox.height)
    }

    func width(withConstraintedHeight height: CGFloat, font: UIFont) -> CGFloat {
    let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height)
    let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin,
    attributes: [NSAttributedStringKey.font: font], context: nil)

    return ceil(boundingBox.width)
    }
    }

希望对您有帮助!

关于ios - heightForRowAt 在 UITableView 中的 cellforRowAt 之后被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47094697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com