首页 文章

tableView节 Headers 消失SWIFT

提问于
浏览
24

我有一个tableView设置,以便在触摸单元格时,它会在高度上展开以显示更多信息 . tableView有5个部分 .

我有一个错误:当一个单元格扩展时,该单元格下面的所有headersCells都不可见 . 控制台输出以下内容:“[31233:564745]没有重用表格单元格的索引路径”

在我的故事板中,我有2个自定义单元格:“myCell”用于数据承载单元格,“headerCell”用于 Headers .

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(),它会正常运行,尽管 Headers 背景变黑,并且失去动画 .

我有所有 Headers 声明的东西:func tableView(tableView:UITableView,viewForHeaderInSection section:Int) - > UIView?

我错过了什么?我真的很喜欢tableView仍然有动画,并保持背景clearColor() .

提前致谢 . 我确实读过目标C答案,但无法让他们为我工作 . 我非常感谢SWIFT的一些帮助 .

我认为问题是表单元格被重用的无索引路径 .

6 回答

  • 5

    我在控制台输出中找到了答案 . 在头函数中使用此代码:

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
    

    请勿返回 headerCell 或您的可重复使用标识符 . 返回 reuseIdentifier.contentView . 对我而言: return headerCell!.contentView .

  • 90

    只是补充一点,我感到困惑的是,为什么我不能参考我的单元格的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
    }
    
  • 1

    由于我还没有达到50的声誉,我无法评论之前的答案,所以我为将此列为另一个答案而道歉 .

    返回ContentView将使该函数工作,但将删除对reuseIdentifier(headerCell)所做的所有格式化

    headerCell.backgroundColor = UIColor.cyanColor()
    

    这不会为headerCell提供青色

    要解决此问题,只需将“.contentView”添加到格式化行

    headerCell.contentView.backgroundColor = UIColor.cyanColor()
    
  • 0

    当我将应用程序转换为IOS 10时,2个表中的表视图 Headers 消失了 - 我在Apple开发人员API文档中找到了关于表 Headers 的原因 . 当我添加以下内容时,丢失的 Headers 重新出现!

    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
    {
    return 44 // where 44 is the header cell view height in my storyboard
    }
    
  • 14

    我有同样的错误,因为我使用dequeue方法而不是 UITableViewHeaderFooterView 返回一个单元格 .

    解决方案:

    • 在视图层次结构之外添加视图

    • 将类型设置为 UITableViewHeaderFooterView

    • 自定义

    • 链接到 @IBOutlet

    • func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? 返回插座

    常见陷阱:

    不要忘记设置 Headers 大小不要忘记将插座设置为强 .

  • 9

    你可以将tableviewcell包装在UIView中

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
    let containerView = UIView()
    guard let headerCell = tableView.dequeueReusableCell(withIdentifier: "MyHeaderView") as? MyHeaderView else { fatalError(" Failed to load MyHeaderView") }
    containerView.addSubview(headerCell)
    return containerView
    

    }

相关问题