如何在swift 2中的UITableViewController中自动调整表格中的rowheight?

时间:2016-07-14 05:00:05

标签: ios swift uitableview

我试图在UITableViewController中以编程方式调整行高。我试过这个

class MyProfile: UITableViewController {

    var details:[String] = ["collection","of","titles","are","written","here"]
    var subdetails:[String] = ["it", "is","a" ,"big", "string", "list"]
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.estimatedRowHeight = 88.0
        tableView.tableFooterView = UIView()
    }

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

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

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let reuseIdentifier = "eachCellProfile"
        var cell:UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as UITableViewCell?
        if (cell != nil) {
            cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: reuseIdentifier)
        }
        cell!.detailTextLabel?.font = UIFont.boldSystemFontOfSize(16.0)
        cell?.detailTextLabel?.numberOfLines = 10

        cell!.textLabel?.text = self.details[indexPath.row]
        cell!.detailTextLabel?.text = self.subdetails[indexPath.row]
        return cell!
    }

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

我没有在表格单元格中添加任何限制。我想根据标题和标题详细信息字符串的长度和字体大小设置行高。我怎么能这样做?

1 个答案:

答案 0 :(得分:3)

使用./function_test.sh argument1 argument2 计算每个单元格创建前的高度。使用将用于查找高度的标题详细信息字符串并将其添加到默认高度。在我的情况下,单元格的默认高度为85,字体大小为15.

heightForRowAtIndexPath

根据其含量,这给了我动态细胞高度。希望这有帮助