ios - 具有动态 subview swift 的 UITableViewCell 高度

标签 ios swift uitableview

我有一个 UITableViewUITableViewCell 有一个动态的 UILabel,它将根据我从数据库中获取的数据添加。

我正在使用类似的东西

let testing = ["A", "B", "C", "D"] // This array is dynamic data
self.dictionaryTableView.estimatedRowHeight = 44
self.dictionaryTableView.rowHeight = UITableViewAutomaticDimension

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "vocabCell", for: indexPath)
    var y = 0
    for var i in 0..<self.testing.count {
        let lbl = UILabel(frame: CGRect(x: 0, y: y, width: 50, height: 25))
        lbl.text = self.testing[i]
        lbl.numberOfLines = 0
        cell.addSubview(lbl)
        y += 20       
    }
    return cell
}

但是 UITableViewCell 高度不会自动拉伸(stretch)以显示所有内容单元格。请帮忙 这是结果 enter image description here

编辑 我在 UITableView cellForRowAtIndexPath 中为 uilabel 添加了约束,约束起作用了(我在扩展单元格高度时就知道了),但单元格高度不会自动拉伸(stretch)

var bottomAnchor: NSLayoutYAxisAnchor = NSLayoutYAxisAnchor()
for var i in 0..<self.testing.count {
    let lbl = UILabel()
    lbl.text = self.testing[i]
    lbl.numberOfLines = 0
    lbl.translatesAutoresizingMaskIntoConstraints = false

    cell.addSubview(lbl)
    lbl.leftAnchor.constraint(equalTo: cell.leftAnchor, constant: 16).isActive = true
    lbl.widthAnchor.constraint(equalTo: cell.widthAnchor).isActive = true
    lbl.heightAnchor.constraint(equalToConstant: 25).isActive = true

    if i == 0 {
        lbl.topAnchor.constraint(equalTo: cell.topAnchor).isActive = true
    } else {
        lbl.topAnchor.constraint(equalTo: bottomAnchor).isActive = true
    }
    bottomAnchor = lbl.bottomAnchor
}
return cell

非常感谢

最佳答案

您需要在实现中解决两件事:

  1. tableView(_:, cellForRowAt:)将单元格出列之前创建单元格的布局。

    出于效率原因,表格 View 在用户滚动时一遍又一遍地重用相同的单元格。这就是为什么您使用这个奇怪的“出队”函数而不是简单地实例化一个新单元格的原因。

    let cell = tableView.dequeueReusableCell(withIdentifier: "vocabCell", for: indexPath)
    

    将始终尝试返回刚刚滚出 View 的已用单元格。仅当没有可用的回收单元格时(例如,当前几个单元格出队时), TableView 才会创建单元格的新实例。

    单元格的回收正是您永远不要tableView(_:, cellForRowAt:) 中创建单元格布局的原因,例如通过添加标签。当单元格被重复使用时,另一个标签将添加到您之前添加的标签之上,当它被第二次重复使用时,您最终会得到三个重叠的标签等。

    这同样适用于约束。当您向 tableView(_:, cellForRowAt:) 中的单元格添加约束而不删除现有约束时,将在用户滚动时添加越来越多的约束,很可能导致严重的约束冲突。

    相反,在出队之前设置单元格的布局。有几种方法可以实现这一点:

    • 如果您在 Storyboard 中使用 UITableViewController,您可以创建动态原型(prototype) 并直接在 Storyboard 中布置单元格。例如,您可以将标签拖到那里的原型(prototype)单元格,并在自定义 UITableViewCell 子类中为其创建一个导出。

    • 您可以为您的单元创建一个 XIB 文件,在 Interface Builder 中打开它并在那里创建您的布局。同样,您需要创建一个具有适当 socket 的 UITableViewCell 子类,并将其与您的 XIB 文件相关联。

    • 您可以创建一个 UITableViewCell 子类并完全在代码中设置您的布局,例如在单元格的初始化程序中。

  2. 您需要使用自动布局。

    如果您在 Interface Builder 中创建布局,则只需添加必要的约束即可。如果您在代码中创建布局,则需要将所有要约束的 View 的 translatesAutoresizingMaskIntoConstraints 属性设置为 false,例如:

    lbl.translatesAutoresizingMaskIntoConstraints = false
    

    在您的特定布局中,您需要使用单元格 contentView 的相应边缘来限制左侧、右侧、顶部和底部的标签。

    如果您不知道如何操作,请阅读 Apple 的 Auto Layout Guide . (在 Interface Builder 中而不是在代码中执行此操作通常是更好的主意。)

    关于如何使用 “UITableView 中的自动布局以实现动态单元格布局和可变行高”的非常详细的说明可以在 here 中找到。 .

关于ios - 具有动态 subview swift 的 UITableViewCell 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46988780/

相关文章:

php - 这是哪种图像格式?

ios - 在选择一些单元格后向上和向下拖动时,我的复选标记消失了

ios - 禁用 UIScrollView 惯性 (scrollViewDidEndDecelerating) Swift

ios - tableView 中 uiimage 的指示器

swift - 如何为 UITableViewCell TextView 对象实现实时字数统计?

ios - 我在哪里在主从应用程序中实例化我的类

ios - 如何在使用 Firebase Auth 创建用户期间验证字段服务器端?

ios - Swift:LongPressGestureRecognizer 和 ScrollView 之间的冲突

ios - 仅限生产版本的 EXC_BREAKPOINT (SIGTRAP)

ios - UITableViewAutomaticDimension 和多行标签问题