【发布时间】: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