根据UILabel高度创建自定义表视图单元格

时间:2016-10-27 21:29:33

标签: ios swift uitableview

我正在使用Swift,并且在使我的表格视图单元格自我调整时遇到了一些问题。从我在网上看到的最好的方法是使用:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 138.0

我已将此代码放入其中但似乎无法正常工作。我在viewDidLoad和cellForRowAt函数中尝试过它。我不确定我是否把它放在错误的地方,或者我是否设置了错误的东西。我的UILabel底部相对于容器底部也有自动约束,期望单元容器会增长,但容器底部的UILabel底部将保持不变。这是我目前在TableViewController中的代码:

class TableViewController: UITableViewController {

var data = [TextData]()

override func viewDidLoad() {
    super.viewDidLoad()
    loadSampleData()   
}


    func loadSampleData() {
        let title1 = TextData(title: "Monday", blurb: "Today is Monday and it's a sunny day", photograph: #imageLiteral(resourceName: "mondayone"), article: "Monday Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.")
        let title2 = TextData(title: "Tuesday", blurb: "Today is Tuesday and it's a bit rainy but will clear up later on today", photograph: #imageLiteral(resourceName: "tuesdaytwo"), article: "Tuesday Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur." )
        let title3 = TextData(title: "Wednesday", blurb: "Today is Wednesday and the umbrella should be handy because it's going to be stormy", photograph: #imageLiteral(resourceName: "wednesdaythree"), article: "Wednesday Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.")
        let title4 = TextData(title: "Thursday", blurb: "Today is Thursday and it's sunny again, no need for any umbrella today!", photograph: #imageLiteral(resourceName: "thursdayfour"), article: "Thursday Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.")

        data += [title1, title2, title3, title4]
    }
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return data.count
}

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellIdentifier = "TitleTextCell"
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! TableViewCell

    let realData = data[(indexPath as NSIndexPath).row]

    cell.titleLabel.text = realData.title
    cell.bodyText.text = realData.blurb
    cell.photo.image = realData.photograph

    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 138.0

    return cell
}

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

以下是我的tableview目前的情况: Current Table View

以下是我目前的限制: Constraints

1 个答案:

答案 0 :(得分:2)

目前照片正在阻止您的手机变高。由于照片被限制在细胞的顶部和中心,因此使细胞更高会使图像更高,但由于图像具有高度约束,照片会阻止细胞变高。

删除Photo.centerY约束应允许基于标签调整单元格大小。或者,您可以将constaraint更改为不等式(≤或≥,不记得哪个)。如果还没有另外一个约束条件,那么应该限制单元格变小。