我有一个包含tableview的视图控制器。表格视图由包含单个标签的自定义单元格组成。我有不同长度的文本,这些文本将显示在这些单元格中。我面临的问题是,单元没有被扩展到适当的高度。我已经尝试了SO中存在的许多解决方案,但是到目前为止,它们都没有起作用。
这是视图控制器的代码

class ViewController: UIViewController {

    @IBOutlet var tableView: UITableView!

    let items = [
        "This is the first text",
        "This is the first text and this is the second text","now you may be thinking where is the third text?. Well, There was the first text and second text, now here is the third text",
        "This is the fourth short and sweet text",
        "Slow down you crazy child, you're so ambitious for a juvenile. If you're so smart, tell me why are you still so afraid.","Where's the fire? What's the hurry about. You better cool it off before you burn it out. There's so much to do and so many hours in a day.You got your passion, got your pride. Don't you know that only fools are satisfied. Dream on but don't imagine that they come true. Don't even realise vienna waits for you"]

    var prototypeCell:CustomCell!

    override func viewDidLoad() {
        super.viewDidLoad()

        //setting up tableview
        self.tableView.allowsSelection = false
        self.tableView.dataSource = self
        self.tableView.delegate = self

        configurePrototypeCell()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func configureCell(cell:CustomCell, forIndexPath indexPath:NSIndexPath)
    {
        cell.itemLabel.text = items[indexPath.row]

    }


    func configurePrototypeCell()
    {
        self.prototypeCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomCell
    }


}

extension ViewController:UITableViewDataSource
{
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomCell
        configureCell(cell, forIndexPath: indexPath)
        return cell

    }

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

extension ViewController: UITableViewDelegate
{

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

        self.prototypeCell.itemLabel.text = items[indexPath.row]
        self.prototypeCell.itemLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.tableView.frame)

        self.prototypeCell.setNeedsLayout()
        self.prototypeCell.layoutIfNeeded()


        let size = self.prototypeCell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
        let height = size.height
        return height


    }

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

}

CustomCell类是UITableViewCell的子类。它包含名为itemLabel的UILabel

最佳答案

请在自定义单元格中尝试此操作,并从TableviewDelegate的height方法中调用它,以计算自定义单元格的高度。

代码:let size = sizingCell.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)

关于ios - iOS 7的动态表格 View 单元格高度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37047001/

10-14 19:37
查看更多