当UITextView大小更改时,如何调整UITableViewCell的大小? [英] How do I make a UITableViewCell resize when a UITextView size changes?
问题描述
我遇到了带有SnapKit的自动版式问题.我向UITableViewCell中添加了一个UITextView,并且我希望在用户添加内容时该单元格随着文本视图的扩展而扩展.但是,实际上,当文本视图尝试扩展时,单元格约束会中断.这是我的代码:
I'm having an Auto Layout with SnapKit issue. I added a UITextView to a UITableViewCell, and I want the cell to expand as the text view expands when the user adds content. However, in reality, when the the text view attempts to expand, the cell constraints break. Here is my code:
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)
时,它仍然不起作用.
The cell never resizes, because the UITableViewCellContentView:0x13fb03830.height
constraint isn't changed. Shouldn't this be changed by the system to the new calculated height? When I set .priority(.high)
to every constraint, it still doesn't work.
推荐答案
在没有一点帮助的情况下,表视图和单元格不会自动调整大小.
Table views and cells do not auto-size without a little help.
像这样更改您的单元格类别:
Change your cell class like this:
// 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
:
Then, in your controller class, change cellForRowAt
as follows:
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
}
这篇关于当UITextView大小更改时,如何调整UITableViewCell的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!