Ios Swift 3-从xib加载的UITableViewCell-自动布局和高度不正确

Ios Swift 3-从xib加载的UITableViewCell-自动布局和高度不正确,ios,swift,xcode,uitableview,swift3,Ios,Swift,Xcode,Uitableview,Swift3,我有一个关于UITableViewCell的问题,它是从xib加载到表中的。我设计了一个具有自定义高度和我的东西的xib电池。我有自己的桌子: class MyTableView: UITableView, UITableViewDataSource, UITableViewDelegate{ let sections: [String] = ["Section 1", "Section 2", "Section 3"] let s1Data: [String] = ["Row

我有一个关于UITableViewCell的问题,它是从xib加载到表中的。我设计了一个具有自定义高度和我的东西的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!

    }

}
正如你所看到的,一切都是非常基本的,我还没有清理它(只是展示)。该部分工作正常,显示正确。但迈塞尔完全错了。行本身没有应用高度(因此内容或多或少被剪切),我在xib中设置的自动布局也不存在。只是一团糟

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

我还尝试了以下两行:

    rowHeight = UITableViewAutomaticDimension
    estimatedRowHeight = 96.0
在xib中,我设置了正确的约束,以强制单元展开

tableview本身被放置在ViewController中,并被配置为MyTableView类

那么,为什么高度和自动布局不应用于单元?有人能给我一个建议吗

编辑:

这是单元格的xib文件:

这是表格的结果:


正如您所看到的,autolayout功能现在可以正常工作了。可能是我这边有故障,但高度仍然不正确

听起来好像在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

删除您的:

rowHeight=UITableViewAutomaticDimension

estimatedRowHeight=96.0

将此方法用于根据需要设置表格每行的高度:

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


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

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


为什么不简单地
行高=96
?但是如果OP想要基于各种因素更改
行高
,这就可以了,否则简单的
行高=96
就足够了。我同意委派方法更灵活,但是对于静态高度
rowHeight
很简单如果您希望静态高度而不是让autolayout为您计算,请将
rowHeight=96
设置得有点过分,为什么要使用autolayout来计算高度,然后将其强制为常量?