在 UITableViewCell 中使用 UITextView 动态调整单元格大小会导致警告? [英] Dynamic Cell Re-sizing with UITextView within UITableViewCell causes warning?

查看:20
本文介绍了在 UITableViewCell 中使用 UITextView 动态调整单元格大小会导致警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前在我的代码中,我正在以编程方式在 UITableViewCell 中添加一个 UITextView 并添加了根据输入的内容数量自动调整单元格大小的功能用户.

Currently in my code, I'm programmatically adding a UITextView within a UITableViewCell and added the ability to auto-resize the cell based on how much content is typed by the user.

目前看起来是这样:

override func viewDidLoad()
{
    tableView.estimatedRowHeight = 30.0
    tableView.rowHeight = UITableViewAutomaticDimension
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    // Dequeue the cell to load data
    let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

    if indexPath.section == 0
    {
        // Code
    }
    if indexPath.section == 1
    {            
        let textView: UITextView = UITextView()

        textView.delegate = self
        textView.textColor = UIColor.black
        textView.isScrollEnabled = false
        textView.translatesAutoresizingMaskIntoConstraints = false

        cell.addSubview(textView)

        let leadingConstraint = NSLayoutConstraint(item: cell.contentView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: textView, attribute: NSLayoutAttribute.leading, multiplier: 1.0, constant: 8.0)

        let trailingConstraint = NSLayoutConstraint(item: cell.contentView, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: textView, attribute: NSLayoutAttribute.trailing, multiplier: 1.0, constant: -8.0)

        cell.contentView.addConstraint(leadingConstraint)
        cell.contentView.addConstraint(trailingConstraint)

        let topConstraint = NSLayoutConstraint(item: cell.contentView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: textView, attribute: NSLayoutAttribute.top, multiplier: 1.0, constant: 0)

        let bottomConstraint = NSLayoutConstraint(item: cell.contentView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: textView, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: 0)

        cell.contentView.addConstraint(topConstraint)
        cell.contentView.addConstraint(bottomConstraint)
    }
    else if indexPath.section == 2
    {
        // Code
    }

    return cell
}

override func numberOfSections(in tableView: UITableView) -> Int
{
    return 3
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return 1
}

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
    let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell")


    let titleLabel = headerCell?.viewWithTag(1) as! UILabel

    if section == 0
    {
        titleLabel.text = "Title"
    }
    else if section == 1
    {
        titleLabel.text = "Title"
    }
    else if section == 2
    {
        titleLabel.text = "Title"
    }

    return headerCell?.contentView
}

override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView?
{
    let footerCell = tableView.dequeueReusableCell(withIdentifier: "FooterCell")

    return footerCell
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
    if indexPath.section == 1
    {
        return UITableViewAutomaticDimension
    }

    return super.tableView(tableView, heightForRowAt: indexPath)
}

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
{
    return 35.0
}

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat
{
    return 15.0
}

func textViewDidChange(_ textView: UITextView)
{
    tableView.beginUpdates()
    tableView.endUpdates()
}

我只关心第 1 部分,因为这是我想要自动调整单元格高度的唯一单元格.所有其他部分应保持静态单元格高度.

I only care about section 1 because that's the only cell that I want to auto-resize the cell's height. All other sections should maintain a static cell height.

我遇到的问题是,一旦我在 UITextView 中第一次按下 Enter 键跳到下一行,我很快就会看到一些幽灵"单元格并得到一个警告说明:

The problem I have is that as soon as I press the enter key for the FIRST time in the UITextView to skip to the next line, I see very quickly some "ghost" cells and get a warning stating:

没有重用表格单元格的索引路径

no index path for table cell being reused

单元格的高度会相应地动态重新调整大小,在按下初始返回键后,任何后续的新行都不会重新生成幽灵"单元格,但我仍然收到警告.

The cell's height DOES dynamically re-size itself accordingly and after the initial return key is pressed, any subsequent new lines does not re-produce the "ghost" cells, but I still get the warnings.

我好像做错了什么?

谢谢

推荐答案

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
 if(indexPath.row == 0){
  return heightForFirstRow
 }
return heightForStaticCell
}

这篇关于在 UITableViewCell 中使用 UITextView 动态调整单元格大小会导致警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆