UITableView.automaticDimension 不工作 tableView detailTextLabel [英] UITableView.automaticDimension Not working tableView detailTextLabel

查看:29
本文介绍了UITableView.automaticDimension 不工作 tableView detailTextLabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以编程方式实现 UITableViewController(没有故事板!).

I am implementing UITableViewController Programmatically (No Storyboards!).

我尝试了许多可能的方法来实现 TableViewCell 的 detailTextLabel 的自动调整大小,但都没有奏效.我不知道我错过了什么,或者它是否是一个错误!这是我尝试过的:

I tried many possible ways to implement automatic resizing of TableViewCell's detailTextLabel but none is working. I don't know what I am missing or whether it's a bug! Here's what I tried:

//Class - tableViewContoller
override func viewDidLoad() {
    super.viewDidLoad()
    setUpTableView()
}


func setUpTableView() {
    tableView.tableFooterView = UIView(frame: CGRect.zero)
    tableView.separatorColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 0.8)
    tableView.contentInset = UIEdgeInsets(top: 10.0, left: 0.0, bottom: 10.0, right: 0.0)
    tableView.dataSource = self
    tableView.delegate = self
    tableView.rowHeight = UITableView.automaticDimension
    tableView.estimatedRowHeight = UITableView.automaticDimension //Tried 44 -> Not working either
    tableView.reloadData()
}
//cellForRowAt IndexPath
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: cellId)
    if cell == nil {
        cell = UITableViewCell(style: UITableViewCell.CellStyle.value1, reuseIdentifier: cellId)
    }
    cell?.detailTextLabel?.numberOfLines = 0
    cell?.detailTextLabel?.lineBreakMode = .byWordWrapping
    cell?.selectionStyle = .none
    switch indexPath.row {
    case 0:
        cell?.textLabel?.text = "Case 1"
        cell?.detailTextLabel?.text = caseDetails?.details
    case 1:
        cell?.textLabel?.text = "Case 2"
        cell?.detailTextLabel?.text = caseDetails?.bio
    default:break
    }
    return cell!
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
}

我有 2-3 个单元格,其中 detailTextLabel 可能有多行.请让我知道我在这里缺少什么.我在 Internet 上阅读后想到的是添加自定义约束,但我认为这也行不通.

I have 2-3 cells where detailTextLabel may have multiple lines. Please let me know what's that I'm missing here. What I figured after reading on the Internet is to add custom constraints, but I don't think that'll work either.

推荐答案

你必须为那个 cell?.detailTextLabel

cellForRowAt

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    var cell = tableView.dequeueReusableCell(withIdentifier: cellId)
    if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: cellId)
    }
    cell?.detailTextLabel?.numberOfLines = 0
    cell?.detailTextLabel?.lineBreakMode = .byWordWrapping
    cell?.selectionStyle = .none

    cell?.detailTextLabel?.layer.backgroundColor = UIColor.yellow.cgColor
    // ALLOW MANUAL CONSTRAINTS
    cell?.detailTextLabel?.translatesAutoresizingMaskIntoConstraints = false
    // TOP +15, BOTTOM -15, RIGHT -15
    cell?.detailTextLabel?.topAnchor.constraint(equalTo: (cell?.contentView.topAnchor)!, constant: 15).isActive = true
    cell?.detailTextLabel?.bottomAnchor.constraint(equalTo: (cell?.contentView.bottomAnchor)!, constant: -15).isActive = true
    cell?.detailTextLabel?.rightAnchor.constraint(equalTo: (cell?.contentView.rightAnchor)!, constant: -10).isActive = true

    switch indexPath.row {
    case 0:
        cell?.textLabel?.text = "Case 1"
        cell?.detailTextLabel?.text = "hi\nhello\nwelcome\nhow are you"
    case 1:
        cell?.textLabel?.text = "Case 2"
        cell?.detailTextLabel?.text = "caseDetails?.bio\n\n\n123456"
    default:break
    }
    return cell!
}

输出

这篇关于UITableView.automaticDimension 不工作 tableView detailTextLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆