Swift UITableViewCell с повторно используемыми компонентами - PullRequest
0 голосов
/ 11 мая 2018

Я пытаюсь написать UITableViewCell программно с повторно используемыми компонентами, которые я называю «картами».
«Карты» используют AutoLayout.

Я создал класс UITableViewCell:

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        let card = NoticeCardView()
        contentView.addSubview(card)

        card.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 5).isActive = true
        card.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 5).isActive = true
        card.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -5).isActive = true
        card.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5).isActive = true
}

func setValues(notice : Notice) {
        card.setValues(notice: notice)
}  

Я создаю ячейку в контроллере

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell : NoticeListCell = tableView.dequeueReusableCell(withIdentifier: "noticeCell", for: indexPath) as! NoticeListCell

        let notice = notList[indexPath.row]
        cell.setValues(notice: notice)
        return cell;
    }  

В результате «карта» отображается правильно, но «ячейка» сохраняет первоначальную высоту 44, то есть значение, установленное UITableViewAutomaticDimension.
Если я добавлю UITableViewCell, то все определение карты будет работать хорошо, но у меня нет компонента для повторного использования.
У кого-то есть идея или вы использовали такой дизайн?

1 Ответ

0 голосов
/ 11 мая 2018

Проблема в том, что вы забыли установить для translatesAutoresizingMaskIntoConstraints карты значение false.

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
  super.init(style: style, reuseIdentifier: reuseIdentifier)

  let card = NoticeCardView()
  // Add this line and it will work
  card.translatesAutoresizingMaskIntoConstraints = false
  contentView.addSubview(card)

  card.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 5).isActive = true
  card.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 5).isActive = true
  card.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -5).isActive = true
  card.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5).isActive = true
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...