UITableView.automaticDimension Не работает tableView detailTextLabel - PullRequest
0 голосов
/ 02 декабря 2018

Я реализую UITableViewController Программно (без раскадровок!).

Я перепробовал много возможных способов реализовать автоматическое изменение размера TableViewCell detailTextLabel, но ни один из них не работает.Я не знаю, что мне не хватает или это ошибка!Вот что я попробовал:

//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 может иметь несколько строк.Пожалуйста, дайте мне знать, что мне здесь не хватает.После чтения в Интернете я понял, что добавляет пользовательские ограничения , но я не думаю, что это тоже сработает.

Ответы [ 2 ]

0 голосов
/ 02 декабря 2018

Для этого необходимо добавить ограничения 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!
}

Выход

enter image description here

0 голосов
/ 02 декабря 2018

Должно работать, если вы установите приблизительную высоту строки в фактическое значение.Вы не можете установить оба значения на .automaticDimension.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...