【问题标题】:Make tableview cell expand with textView text size使用 textView 文本大小使 tableview 单元格扩展
【发布时间】: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


【解决方案1】:

给你的文本视图添加一个最小高度约束并使用第一种方法怎么样?只要确保您的UITextView 的滚动被禁用。 因为您在第一个解决方案中遇到的唯一问题是单元格高度太小,而里面什么都没有...

否则你的代码看起来很不错,可能会给你流畅的动画。

【讨论】:

  • 为此,我使用了大小为 48 的高度约束和“大于或等于”选项。在这种情况下,文本视图第一次显示高度为 48,但不随文本扩展。我只是在带有顶部、底部、前导和尾随约束的表格单元格上放置了一个文本视图。
  • 当我将大小为 48 的高度约束设置为“大于或等于”选项时,我不知道为什么单元格没有随输入的文本扩展。
  • 也许是因为一开始没必要。您是否尝试继续,并添加更多文本。你也应该在你的文本视图中禁用滚动
  • 是的,在禁用 textview 的滚动后它开始工作。谢谢
【解决方案2】:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  return UITableViewAutomaticDimension 
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { 
  return yourinitialheight 
}

移除高度约束,设置顶部、底部、前导和尾随约束。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 2023-03-11
    相关资源
    最近更新 更多