Swift:当用户在UITextView中输入时,tableViewCell的持续调整大小

3

介绍

背景:

我正在创建一个应用程序,但遇到了一个问题,我无法解决。

  • 我有一个tableView,其中包含许多UI元素的单元格。所有约束都使用EasyPeasy库完成,该库基本上只设置自动布局约束(我也尝试手动设置它们)。所讨论的UITextView通过各种数字被约束到左侧、右侧、顶部和底部,但我没有对其高度或宽度进行约束。

  • 在cellForRowAt indexPath中:我为每个单元格的textView设置了self委托,使用了在单元格自定义类中声明的delegate属性。我还使用其单元格indexPath.row来标记每个textView(在textViewDidChange方法中给出textView.tag整数)。

问题/认识:

  • 浏览了很多SO后,我找到了一些类似这样的问题,但我无法使它们对我起作用,我已经实现了它们的一部分,感觉对我的情况很有逻辑。我认为,区别于那些问题的问题在于,为了使我的cell设计工作,cell必须具有itemHeight或更高的高度。
  • 我注意到,当我输入文本时,文本视图本身会增加高度(甚至超过了单元格的边框,但由于达到该点而不可见),但是单元格本身不会调整大小。
  • 我尝试了一个只包含textView的单元格,因此问题必须在textViewDidchange或heightForRowAt indexPath方法中。

问题:

我在这里做错了什么?为什么单元格的高度不随我在textView中输入而动态更改?

代码:

    func textViewDidChange(_ textView: UITextView) {
       var newframe = textView.frame
       newframe.size.height = textView.contentSize.height - textView.frame.size.height + itemHeight[textView.tag]
       textView.frame = newframe
       let ndxPath = IndexPath(row: textView.tag, section: 0)
       let cell = tableView.cellForRow(at: ndxPath) as! EventsCell
       cell.frame = CGRect(x: cell.frame.origin.x, y: cell.frame.origin.y, width: cell.frame.width, height: textView.frame.height)

         tableView.beginUpdates()
         tableView.setNeedsLayout() //have tried without this line
         tableView.layoutIfNeeded() //have tried without this line
         tableView.endUpdates()

    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if UITableViewAutomaticDimension > itemHeight[indexPath.row] {
            return UITableViewAutomaticDimension
        } else {
            return itemHeight[indexPath.row]
        }
    }

TextView约束条件:

   let containerView : UIView = {
        let cv = UIView(frame: .zero)
        cv.backgroundColor = .white
        cv.translatesAutoresizingMaskIntoConstraints = false
        cv.layer.cornerRadius = 7
        return cv
    }()

    let eventText : GrowingTextView = {   // GrowingTextView is a extension to a regular UITextView
        let tv = GrowingTextView()
        tv.allowsEditingTextAttributes = true
        tv.isScrollEnabled = false
        var delegate: UITextViewDelegate?
        tv.textContainerInset = UIEdgeInsetsMake(1, 1, 0, 1)
        tv.autoresizingMask = .flexibleHeight
        tv.translatesAutoresizingMaskIntoConstraints = false
        return tv
     }()

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

    containerView.addSubview(eventText)
    contentView.addSubview(containerView)

    containerView .easy.layout([Height(CGFloat(95 * itemCount)), Left(8), Right(8)])
    eventText .easy.layout([Left(77), Right(5), Top(90), Bottom(4)])

 }

感谢阅读我的帖子。

你是否将textView底部连接到单元格底部? - Rakesha Shastri
@RakeshaShastri 它是自动布局的,距离一个UIView底部4像素,该UIView的自动布局与单元格contentView的确切框架相同。概念代码:UIView.addSubview(textView) 然后 contentView.addSubview(textView)。我已经在编辑中添加了textView的约束代码。 - theoadahl
@RakeshaShastri 我尝试直接将其附加到contentView,但它仍然没有扩展单元格(我分配了contentView.backgroundColor = .red来检查,但它没有扩展,但是textView的文本超过了下一个单元格)。也许我的textViewDidChange方法有问题? - theoadahl
你尝试过只使用textView吗? - Rakesha Shastri
@RakeshaShastri 我只尝试了textView,但它并没有改变单元格的高度。因此可能是textViewDidChange方法或heightForRowAt indexPath方法有误。如果可以的话,您能再指导一下吗? - theoadahl
显示剩余2条评论
1个回答

4
  • 高度限制的约束条件应该被布置在这样一种方式中,使得 textView 直接连接到 contentView 的顶部和底部或连接到与 contentView 顶部和底部相连的视图上,以便自动布局可以通过这些约束条件计算出高度。
  • 确保不为 textView 指定高度并且禁用滚动。让自动维度来处理所有的事情。
  • 现在,您只需要在 textViewDidChange 中调用 tableView.beginUpdates()tableView.endUpdates() 即可。

这是我的repo,展示了同样的内容。


OP编辑:

  • You should store the additional height that you add in a variable in the cell class so the cells can reload an appropriate height when the tableVIew is reloaded.

  • You should also change textViewDidChange method

    cell.frame = CGRect(x: cell.frame.origin.x, y: cell.frame.origin.y, width: cell.frame.width, height: textView.frame.height) 
    

    to

    let newFrame = ”originalCellHeight” - ”originalTextViewHeight” + textView.contentSize.height
    
    cell.frame = CGRect(x: cell.frame.origin.x, y: cell.frame.origin.y, width: cell.frame.width, height: newFrame )`
    

标记为已回答,来自聊天讨论中的文本 - theoadahl
你能否编辑并添加哪一部分帮助了你得出答案,以便帮助其他人? - Rakesha Shastri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接