【问题标题】:tableView section headers disappear SWIFTtableView 部分标题消失 SWIFT
【发布时间】:2015-07-20 21:18:38
【问题描述】:

我设置了一个 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 的帮助。

我认为问题在于重复使用表格单元格的无索引路径。

【问题讨论】:

    标签: ios uitableview swift header nsindexpath


    【解决方案1】:

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

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

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

    【讨论】:

    • 在这种情况下我得到透明的标题视图。
    • 如果要在单元格中添加按钮或手势怎么办?这行不通,因为contentView 对此一无所知。
    • 是的,如果我有一个按钮,它就不起作用......你找到这个 eMdOS 的解决方案了吗?
    • 当我返回 header.contentView 时,我的所有视图都变得不可见
    • 在 contentView 上而不是在单元格/视图本身上添加点击手势,然后它将起作用。
    【解决方案2】:

    由于我的声望还不到 50,我无法对上一个答案发表评论,因此我很抱歉将其列为另一个答案。

    返回 ContentView 将使函数正常工作,但会删除对重用标识符(headerCell)所做的所有格式

    headerCell.backgroundColor = UIColor.cyanColor()
    

    这不会为您的 headerCell 提供青色

    要解决这个问题,只需将“.contentView”添加到您的格式行

    headerCell.contentView.backgroundColor = UIColor.cyanColor()
    

    【讨论】:

      【解决方案3】:

      补充一点,当我可以很清楚地看到它在那里时,为什么我不能引用单元格的 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
      }
      

      【讨论】:

      • 如果类在 UITableViewController 中扩展,则无需添加 UITableViewHeaderFooterView。谢谢你的 headerCell!.contentView 回答
      • 使用 UITableViewHeaderFooterView 不是强制性的,我使用的是 UITableViewCell 类,但关键是返回 cell.contentView 而不是单元格本身。归还整个单元格对我来说是个问题。
      • 添加 .contentView 节省了我的时间
      • 这是最好的答案!永远不要让你的标题像细胞一样!
      【解决方案4】:

      当我将我的应用程序转换为 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
      }
      

      【讨论】:

      • 这似乎只在 iOS 10 中出现,在 11/12 中不需要
      【解决方案5】:

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

      解决办法:

      • 在视图层次结构之外添加一个视图
      • 将类型设置为UITableViewHeaderFooterView
      • 自定义
      • 链接到@IBOutlet
      • func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?返回出口

      常见的陷阱:

      不要忘记设置标题大小 不要忘记将出口设置为强。

      【讨论】:

        【解决方案6】:

        您可以将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
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-27
          • 1970-01-01
          • 2023-03-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多