加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 默认UITableView Cell自动高度

发布时间:2020-12-14 05:30:10 所属栏目:百科 来源:网络整理
导读:我有一个UITableViewController,其样式为uitableviewcellstylesubtitle,其中字幕标签的numberoflines = 0 如果我这样做 tableView.rowHeight = UITableViewAutomaticDimensiontableView.estimatedRowHeight = 44.0 细胞不做自动高度,它真的是真的 – 你不能
我有一个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 {

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读