【问题标题】:Dynamic Cell Re-sizing with UITextView within UITableViewCell causes warning?在 UITableViewCell 中使用 UITextView 动态调整单元格大小会导致警告?
【发布时间】:2017-03-28 06:04:36
【问题描述】:

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

目前看起来是这样的:

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 部分,因为这是我想要自动调整单元格高度的唯一单元格。所有其他部分应保持静态单元格高度。

我遇到的问题是,当我在UITextView 第一次按下回车键以跳到下一行时,我很快就会看到一些“幽灵”单元格并收到警告说明:

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

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

我似乎做错了什么?

谢谢

【问题讨论】:

  • 如果您将let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 行更改为不使用indexPath,警告和幽灵单元格是否会消失,如下所示 - let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell")
  • @Fahim 无效
  • @Pangu 你的输出到底想要什么?
  • @pangu 我认为您必须在 indexPath.section 1 的 heightForRowAtIndexPath 方法中设置动态 UITextview 的框架

标签: ios swift uitableview swift3


【解决方案1】:

TextView 继承自 scrollView,因此 textView 的内容大小可以大于 frame(在这种情况下 textView 会滚动)。为了让 tableView 计算单元格的自动大小,它需要具有隐式大小的组件,如标签或按钮。因此,为了让 tableView 计算 textView 的隐式大小,您应该禁用滚动。

textView.isScrollEnabled = false

应该允许你的单元格扩展:)

编辑:

当一个可重复使用的单元格被用作节标题时,看起来会发生这种情况(就像你的情况一样)看看这个:What is the meaning of the "no index path for table cell being reused" message in iOS 6/7?

建议:

创建一个普通的 xib 添加一个视图并将其作为节标题返回 否则按照上面链接中发布的答案

【讨论】:

  • 抱歉忘记包含该代码,但它在里面
  • 以下建议的编辑也没有解决我的问题
【解决方案2】:

试试这个。 也添加这个方法。 func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat

【讨论】:

  • 没有必要添加,因为OP已经在viewDidLoad中设置了estimatedRowHeight
【解决方案3】:
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
 if(indexPath.row == 0){
  return heightForFirstRow
 }
return heightForStaticCell
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 2014-11-05
    • 2014-01-10
    • 1970-01-01
    • 2015-11-02
    • 2016-06-23
    相关资源
    最近更新 更多