swift – 默认UITableView Cell自动高度

前端之家收集整理的这篇文章主要介绍了swift – 默认UITableView Cell自动高度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个UITableViewController,其样式为uitableviewcellstylesubtitle,其中字幕标签的numberoflines = 0

如果我这样做

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44.0

细胞不做自动高度,它真的是真的 – 你不能在默认的样式细胞上使用它吗?

编辑:

人口简单

override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath)
    let thisHelpArticle = helpObjects[indexPath.section]

    cell.textLabel!.text = thisHelpArticle.helpHeadline
    cell.detailTextLabel!.text = thisHelpArticle.helpDescription

    return cell
}
UPDATE!
为了使默认单元格子标题自动调整单元格,您必须将两个标签的行数设置为0.否则它不起作用.奇怪的.
cell.textLabel?.numberOfLines = 0
cell.detailTextLabel?.numberOfLines = 0

您可以自动调整默认的sytled单元格大小.在最简单的形式中,我有以下代码.这是自动调整默认样式字幕单元格的大小.

class ViewController: UIViewController {

    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self
        tableView.delegate = self
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 44
    }

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

extension ViewController: UITableViewDataSource {
    func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return 2
    }

    func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath)
        let thisHelpArticle = "Lorem ipsum dolor sit amet,vel porttitor blandit,aliquam tristique vestibulum,enim vel eros fames id,in gravida vestibulum gravida tempor,et vel libero sed mauris. Suspendisse ut placerat viverra dictum,ante ante vel ut vestibulum sollicitudin phasellus. Dictumst adipiscing adipiscing nisl,fusce ut. Ante wisi pellentesque,et aliquam rhoncus eget convallis quam voluptate,ut nec quis,sodales ullamcorper elementum pellentesque sagittis vitae,dolor justo fermentum amet risus. Eu placerat ultricies. Ipsum sodales,massa elit,in neque,sed penatibus gravida,cursus eget. Ut tincidunt at eu,wisi dis vel penatibus eget,volutpat ligula vel tortor morbi feugiat,dui et eiusmod dis sociis. Iaculis lorem molestie laoreet sit,orci commodo,fusce vestibulum sapien,quisque egestas maecenas sed rem in nisl."

        cell.textLabel?.numberOfLines = 0
        cell.detailTextLabel?.numberOfLines = 0

        cell.textLabel!.text = thisHelpArticle
        cell.detailTextLabel!.text = thisHelpArticle

        return cell
    }
}

extension ViewController: UITableViewDelegate {

}
原文链接:https://www.f2er.com/swift/319396.html

猜你在找的Swift相关文章