[英]tableView section headers disappear SWIFT
我有一个 tableView 设置,以便在触摸单元格时,它会扩展高度以显示更多信息。 tableView 有 5 个部分。
我有一个错误:当一个单元格展开时,该单元格下方的所有 headersCells 都将不可见。 控制台输出以下内容:“[31233:564745] 没有重用表格单元格的索引路径”
在我的故事板我有2个定制单元: "myCell"
的数据承载细胞, "headerCell"
为标题。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let thisGoal : GoalModel = goalArray[indexPath.section][indexPath.row]
if self.currentPath != nil && self.currentPath == indexPath {
self.currentPath = nil
} else {
self.currentPath = indexPath
}
tableView.beginUpdates()
tableView.endUpdates()
}
如果我在开始/结束更新之间输入tableView.reloadData()
,它会正常运行,尽管标题背景变黑并失去动画。
我已经声明了标题的所有内容: func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
我错过了什么? 我真的很希望 tableView 仍然有动画,并保持背景clearColor()
。
提前致谢。 我确实通读了客观的 C 答案,但无法让它们为我工作。 我真的很感激一些 SWIFT 帮助。
我认为问题是表单元格没有索引路径被重用。
我在控制台输出中找到了答案。 在标头函数中使用此代码:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
不要返回您的headerCell
或您的可重用标识符。 返回reuseIdentifier.contentView
。 对我来说是: return headerCell!.contentView
。
补充一下,当我可以很清楚地看到它在那里时,我对为什么不能引用我的单元格的 contentView 感到困惑的时间比我应该的要长。 我的自定义类(使用 UITableViewCell 而不是 UITableViewHeaderFooterView)每次都会返回一个致命错误。 因此,请确保在 UITableViewHeaderFooterView 类下设置任何自定义样式,例如:
class CustomHeaderCell: UITableViewHeaderFooterView {
您还需要像这样注册 resuableIdentifer:
tableView.registerNib(UINib(nibName: "HeaderCell", bundle: nil), forHeaderFooterViewReuseIdentifier: "CellHeader")
然后这个坏男孩:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableHeaderFooterViewWithIdentifier("CellHeader") as! CustomHeaderCell!
return headerCell!.contentView
}
由于我的声望还没有达到 50,我无法对之前的答案发表评论,因此我很抱歉将其列为另一个答案。
返回 ContentView 将使该函数工作,但会删除对重用标识符(headerCell)所做的所有格式化
headerCell.backgroundColor = UIColor.cyanColor()
这不会为您的 headerCell 提供青色
要解决此问题,只需将“.contentView”添加到您的格式行
headerCell.contentView.backgroundColor = UIColor.cyanColor()
当我将应用程序转换为 IOS 10 时,2 个表中的表视图标题消失了 - 我在 Apple 开发人员 API 文档中找到了有关表标题的原因。 当我添加以下内容时,丢失的标题又出现了!
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
{
return 44 // where 44 is the header cell view height in my storyboard
}
您可以将TableViewCell
包装在UIView
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let containerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 50)) // 50 = Header height
guard let headerCell = tableView.dequeueReusableCell(withIdentifier: "MyHeaderView") as? MyHeaderView else { fatalError(" Failed to load MyHeaderView") }
headerCell.frame = containerView.bounds
containerView.addSubview(headerCell)
return containerView
}
我有同样的错误,因为我使用 dequeue 方法而不是UITableViewHeaderFooterView
返回一个单元格。
UITableViewHeaderFooterView
@IBOutlet
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
返回插座不要忘记设置标题大小不要忘记将插座设置为坚固。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.