在UITableViewCell中使用UITextViewdynamic调整单元大小会导致警告?

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

目前看起来像这样:

 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第一次按Enter键跳到下一行,我就会很快看到一些“鬼”单元,并得到一个警告:

表格单元没有被重用的索引path

单元格的高度DOES会相应地dynamic调整自己的大小,在按下初始返回键之后,任何后续的新行都不会重新生成“鬼影”单元格,但仍会收到警告。

我似乎在做什么错了?

谢谢

TextViewinheritance自scrollView,因此textView的内容大小可以大于frame(在这种情况下,textView将滚动)。 为了使tableView计算单元格的自动大小,它需要隐含大小的组件,如标签或button。 所以为了让tableView为textView计算隐式大小,你应该禁用滚动。

 textView.isScrollEnabled = false 

应该让你的细胞扩大:)

编辑:

看起来像这样当一个可重复使用的单元格被用作节标题时(如你的情况)看看这个: 在iOS 6/7中,“表格单元格没有被重用的索引path”消息是什么意思?

build议:

创build一个正常的xib添加一个视图,并返回它作为一个部分的头部,否则按照上面的链接发布的答案

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

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