【问题标题】:Swift 3 - UITableViewCell loaded from xib - autolayout and height not correctSwift 3 - 从 xib 加载的 UITableViewCell - 自动布局和高度不正确
【发布时间】:2017-05-11 15:47:13
【问题描述】:

我有一个关于从 xib 加载并放入表中的 UITableViewCell 的问题。我在 xib 中设计了具有自定义高度和我的东西的单元格。我有自己的桌子:

class MyTableView: UITableView, UITableViewDataSource, UITableViewDelegate{

    let sections: [String] = ["Section 1", "Section 2", "Section 3"]
    let s1Data: [String] = ["Row 1", "Row 2", "Row 3"]
    let s2Data: [String] = ["Row 1", "Row 2", "Row 3"]
    let s3Data: [String] = ["Row 1", "Row 2", "Row 3"]

    var sectionData: [Int: [String]] = [:]

    override func awakeFromNib() {
        super.awakeFromNib()

        delegate = self
        dataSource = self

        register(UINib(nibName: "MyCell", bundle: nil), forCellReuseIdentifier: "MyCell")

        register(UINib(nibName: "MySection", bundle: nil), forHeaderFooterViewReuseIdentifier: "MySection")

        sectionData = [0:s1Data, 1:s2Data, 2:s3Data]

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int)
        -> Int {
            return (sectionData[section]?.count)!
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int)
        -> String? {
            return sections[section]
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return sections.count
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let section = self.dequeueReusableHeaderFooterView(withIdentifier: "MySection")

        return section

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        var cell = tableView.dequeueReusableCell(withIdentifier: "MyCell")

        return cell!

    }

}

如您所见,一切都非常基本,我还没有清理它(只是展示)。该部分工作正常并且显示正确。但是 MyCell 是完全错误的。行本身没有应用高度(因此内容或多或少被切割)并且我在 xib 中设置的自动布局不存在。只是一团糟。

在 xib 中,我也设置了行的高度。

我还尝试了以下两行:

    rowHeight = UITableViewAutomaticDimension
    estimatedRowHeight = 96.0

在 xib 中,我设置了正确的约束以强制单元格展开。

tableview 本身只是放置在 ViewController 中并配置为 MyTableView-Class。

那么为什么高度和自动布局没有应用于单元格?有人可以在这里给我一个建议。

编辑:

这是单元格的xib文件:

这是表格的结果:

正如您所见,自动布局现在可以使用了。可能是我这边出了问题,但是高度还是不正确

【问题讨论】:

  • 如果你想要静态高度而不是让自动布局为你计算,设置rowHeight = 96

标签: ios swift xcode uitableview swift3


【解决方案1】:

我有一个解决方案。我只是在背景或标签本身中设置了一个新视图,其高度约束等于单元格高度。所以在我的情况下 96

【讨论】:

  • 有点矫枉过正,为什么要使用自动布局计算高度,然后强制它为常数?
【解决方案2】:

听起来您在 Xib 中有一个实际的单元格,您对其施加了约束等。如果是这种情况,那么您应该将其更改为仅使用标签或您需要的东西。

无论哪种方式,我都建议通过创建自己的类来制作 Xib 的组件。

class DesignCell: UITableViewCell {

     @IBOutlet weak var headerLabel: UILabel!

     public func configure(withText text: String) {
           headerLabel.text = text
     }
}

现在您可以在 tableView 数据源中使用该单元格

let cell = tableView.dequeueReusableCell(withIdentifier: DesignCell.identifier) as! DesignCell
    cell.configure(withText: section[indexPath.item])
    return cell

【讨论】:

    【解决方案3】:

    删除您的:

    rowHeight = UITableViewAutomaticDimension

    estimatedRowHeight = 96.0

    使用此方法根据您的需要为表格的每一行设置高度:

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 96
        
    }
    

    或者

    只需self.tableView.rowHeight = 96 即可为所有单元格设置静态高度。

    【讨论】:

    • 为什么不简单地rowHeight = 96
    • 但是如果 OP 想根据各种因素改变rowHeight,那就可以了,否则简单的rowHeight = 96 就足够了。
    • 我同意委托方法更灵活,但对于静态高度rowHeight 更简单
    猜你喜欢
    • 1970-01-01
    • 2013-10-21
    • 2021-10-12
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多