微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/swift/319396.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐