Как мне изменить размер UITableViewCell при изменении размера UITextView? - PullRequest
0 голосов
/ 21 июня 2019

У меня проблема с автоматической компоновкой с помощью SnapKit.Я добавил UITextView в UITableViewCell и хочу, чтобы ячейка расширялась по мере того, как расширяется текстовое представление, когда пользователь добавляет контент.Однако в действительности, когда текстовое представление пытается раскрыться, ограничения ячейки нарушаются.Вот мой код:

TextFieldTableViewCell:

import UIKit

class TextFieldTableViewCell: FieldTableViewCell {

  let textView = EditableTextView()

  override func setUpViews() {
    super.setUpViews()

    setUpTextView()
  }

  private func setUpTextView() {
    contentView.addSubview(textView)

    textView.setContentHuggingPriority(UILayoutPriority(rawValue: 10000), for: .vertical)
    textView.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 10000), for: .vertical)

    textView.snp.makeConstraints { make in
      make.top.equalTo(fieldLabel.snp.bottom).offset(margin)
      make.leading.equalToSuperview().offset(margin)
      make.trailing.equalToSuperview().offset(-margin)
      make.bottom.equalToSuperview().offset(-margin)
    }
  }

}

EditableTextView:

import UIKit

class EditableTextView: UITextView {

  override func didMoveToSuperview() {
    super.didMoveToSuperview()

    isEditable = true
    isScrollEnabled = false
  }

}

А вот и моя ошибка:

2019-06-20 18:23:10.276724-0500 Campaign Detective[1127:169808] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2019-06-20 18:23:10.278517-0500 Campaign Detective[1127:169808] [MC] Reading from public effective user settings.
2019-06-20 18:23:13.479469-0500 Campaign Detective[1127:169808] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSContentSizeLayoutConstraint:0x2831c90e0 Campaign_Detective.EditableTextView:0x140813200'Yeah I\U2019ll call them later...'.height == 86 Hug:10000 CompressionResistance:10000   (active)>",
    "<SnapKit.LayoutConstraint:0x2831f82a0@FieldTableViewCell.swift#37 UILabel:0x13fb03540.top == UITableViewCellContentView:0x13fb03830.top + 15.0>",
    "<SnapKit.LayoutConstraint:0x2831d1740@TextFieldTableViewCell.swift#20 Campaign_Detective.EditableTextView:0x140813200.top == UILabel:0x13fb03540.bottom + 15.0>",
    "<SnapKit.LayoutConstraint:0x2831d1920@TextFieldTableViewCell.swift#23 Campaign_Detective.EditableTextView:0x140813200.bottom == UITableViewCellContentView:0x13fb03830.bottom - 15.0>",
    "<NSLayoutConstraint:0x2836b5040 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x13fb03830.height == 123.333   (active)>"
)

Will attempt to recover by breaking constraint 
<NSContentSizeLayoutConstraint:0x2831c90e0 Campaign_Detective.EditableTextView:0x140813200'Yeah I'll call them later...'.height == 86 Hug:10000 CompressionResistance:10000   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

Размер ячейки никогда не изменяется, поскольку ограничение UITableViewCellContentView:0x13fb03830.height не изменяется.Разве это не должно быть изменено системой на новую расчетную высоту?Когда я устанавливаю .priority(.high) для каждого ограничения, оно все равно не работает.

Ответы [ 2 ]

0 голосов
/ 21 июня 2019

Табличные представления и ячейки не могут автоматически изменять размер без небольшой помощи.

Измените свой класс ячейки следующим образом:

// add the UITextViewDelegate
class TextFieldTableViewCell: FieldTableViewCell, UITextViewDelegate {

    let textView = EditableTextView()

    // this will allow the cell to "call back" to the controller when the text is edited
    var callback: ((String) -> ())?

    // when the text changes, tell the controller to update the dataSource and the table layout
    func textViewDidChange(_ textView: UITextView) {
        callback?(textView.text!)
    }

    override func setUpViews() {
        super.setUpViews()

        setUpTextView()
    }

    private func setUpTextView() {
        contentView.addSubview(textView)

        // set the delegate so textViewDidChange will be triggered on editing
        textView.delegate = self

        // use default Hugging and Compression
        //textView.setContentHuggingPriority(UILayoutPriority(rawValue: 10000), for: .vertical)
        //textView.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 10000), for: .vertical)

        textView.snp.makeConstraints { make in
            make.top.equalTo(fieldLabel.snp.bottom).offset(margin)
            make.leading.equalToSuperview().offset(margin)
            make.trailing.equalToSuperview().offset(-margin)
            make.bottom.equalToSuperview().offset(-margin)
        }
    }

}

Затем в вашем классе контроллера измените cellForRowAtследующим образом:

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldTableViewCell", for: indexPath) as! TextFieldTableViewCell

    // assuming you have an array of strings for the text views
    cell.textView.text = theData[indexPath.row]

    // when the text in the cell is edited, the cell will "call back"
    // and we can update the data source and tell the tableView to
    // update its layout
    cell.callback = { (str) in
        self.theData[indexPath.row] = str
        self.tableView.beginUpdates()
        self.tableView.endUpdates()
    }

    return cell

}
0 голосов
/ 21 июня 2019

Следующая строка сообщает движку, что вы предпочитаете, чтобы textView сокращался, когда недостаточно места. Вероятно, это не то, что вы хотите

textView.setContentHuggingPriority(UILayoutPriority(rawValue: 10000), for: .vertical)

Если ваш набор tableView.rowHeight = UITableView.automaticDimension, после того, как пользователь закончил редактирование, вы можете просто позвонить

tableView.beginUpdates()
tableView.endUpdates()

и это должно пересчитать высоту ячейки и обновить ее

...