使用Swift的静态表中的动态高度

时间:2017-01-04 18:33:58

标签: swift uitableview static storyboard height

我在storyboard中创建了一个静态表,并希望启用自动表格高度。

class DemoTableViewController: UITableViewController {

@IBOutlet var table: UITableView!
@IBOutlet weak var : UILabel!
override func viewDidLoad() {
    super.viewDidLoad()
    tableView.estimatedRowHeight = 44.0
    tableView.rowHeight = UITableViewAutomaticDimension

    myTextLabel.sizeToFit()
    myTextLabel.numberOfLines = 0
    myTextLabel.text = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."

}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}


}

标签的约束固定在单元格边框上。 几乎相同的代码正在使用动态表。

有人知道如何为静态表启用自动占用吗?

修改
这是GitHub-Repo的链接: https://github.com/pascalherrmann/Swift-Dynamic-Sized-Tables 它包含工作自动调整大小的动态表和不工作的静态表的代码。

编辑2 好的,经过几个小时的尝试后我终于找到了解决方案:它必须是

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

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

所以请记住使用override-Attribute并使用函数heightForRowAt indexPath:IndexPath而不是heightForRowAtIndexPath indexPath:NSIndexPath,如果您遇到同样的问题:)

0 个答案:

没有答案