Swift:具有动态高度的自动布局和表格视图单元格

时间:2015-02-09 22:03:04

标签: uitableview swift ios8

我创建了一个测试项目,以了解具有可变高度的表格单元格。我已经阅读了StackOverflow和其他地方的一些教程,但我仍然做错了什么,而且我不太确定是什么。

如何确保无论字符串长度如何,表格单元格的高度都会发生变化以适应所有文字?

在这里,我使用各种长度的字符串数组设置表视图。

这是我的模拟器显示的内容,以及我的代码:

tableview

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    var items: [String] = ["We", "Aenean lacinia bibendum nulla sed consectetur.", "Vestibulum id ligula porta felis euismod semper. Maecenas sed diam eget risus varius blandit sit amet non magna. Sed posuere consectetur est at lobortis. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Cras mattis consectetur purus sit amet fermentum. Etiam porta sem malesuada magna mollis euismod. Sed posuere consectetur est at lobortis. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.", "Blah", "one two three", "Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam.", "gum gum gum gum", "Blah", "one two three", "Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam.", "gum gum gum gum", "Blah", "one two three", "Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam.", "gum gum gum gum", "Blah", "one two three", "Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam.", "gum gum gum gum", "Blah", "one two three", "Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam.", "gum gum gum gum", "Blah", "one two three", "Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam.", "gum gum gum gum"]

    @IBOutlet var tableView: UITableView!

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

        cell.textLabel?.text = self.items[indexPath.row]

        return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        println("You selected cell #\(indexPath.row)!")
    }



    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.estimatedRowHeight = 130.0
        // Do any additional setup after loading the view, typically from a nib.
    }

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

    override func prefersStatusBarHidden() -> Bool { return true }
}

在我的故事板中,我有一个测试单元设置了一些约束(可能做错了,不确定)。约束似乎是确定这种实现或完全搞砸的关键

contraints

非常感谢任何帮助。谢谢!

2 个答案:

答案 0 :(得分:0)

要在多行标签中使用自动布局,您需要设置preferredMaxLayoutWidth属性(在viewDidLoad或其他地方适当)。这将导致autolayout“做正确的事”并使您的标签扩展为多行标签。见https://developer.apple.com/library/ios/documentation/UIKit/Reference/UILabel_Class/index.html#//apple_ref/occ/instp/UILabel/preferredMaxLayoutWidth

请注意,为了在UITableViewCell中正常工作,您还应该回复heightForRowAtIndexPath - 请参阅https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath

答案 1 :(得分:0)

也可以实现它

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