【发布时间】:2018-09-29 11:38:15
【问题描述】:
为此,我学习了很多教程,但我无法找到解决问题的方法
我想在表格单元格上显示 textView。并且 Cell 应该随着用户在 textView 中输入的文本展开。
我发现有两种方法可以做到这一点
第一个:-
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 30;
func textViewDidChange(_ textView: UITextView!, for indexPath: IndexPath!) {
self.presenter?.valueChanged(section: indexPath.section, value: textView.text)
isChanged = true
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
Make the table view to adjust height automatically. But in this case when there is no data to display in cellForRow at index path then textview height is automatically adjusted to 0 and i was not able to select the text view.
第二种方式:-
移除自动尺寸码
// MARK: TextViewTableViewCellDelegate
func textViewDidChange(_ textView: UITextView!, for indexPath: IndexPath!) {
self.presenter?.valueChanged(section: indexPath.section, value: textView.text)
isChanged = true
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
var ret = DEFAULT_CELL_HEIGHT
if let cell = tableView.dequeueReusableCell(withIdentifier: SECTION_TYPE_FREEFORM) as? TextViewTableViewCell {
cell.textView.text = self.presenter?.cellBody(section: indexPath.section)
cell.textView.sizeToFit()
let width = self.view.frame.size.width - HORIZONTAL_PADDING
let calculatedHeight = cell.textView.sizeThatFits(CGSize(width: width, height: CGFloat(MAXFLOAT))).height + 10
if(calculatedHeight > ret) {
ret = calculatedHeight
}
}
return ret
}
但在此我检查了然后我在 heightForRowAtIndexPath 方法中创建单元格然后 textview 高度不合适。而且自动调整高度也不能正常工作。
请提出相同的解决方案。提前致谢。
【问题讨论】:
-
使用自动布局,func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{ return UITableViewAutomaticDimension }
-
为此我使用了大小为 48 的高度约束和“大于或等于”选项。在这种情况下,文本视图第一次显示高度为 48,但不随文本扩展。我只是在带有顶部、底部、前导和尾随约束的表格单元格上放置了一个文本视图。
-
@VasanthanPrem 一旦我删除高度约束,它只会使 textview 高度为 0。我删除了高度约束并使用了上述两种方法,并且 textview 高度显示为 0
-
如果 textview 仅用于阅读目的,请使用标签
-
不,如果我已经有数据,那么我必须在开始时显示它并让用户在 texview 中编辑或添加数据
标签: ios swift uitableview textview uitableviewautomaticdimension