ios - dequeueReusableHeaderFooterViewWithIdentifier 为单元格返回 nil,但 dequeueReusableCellWithIdentifier 不是

标签 ios swift uitableview

我在 Interface Builder 中使用原型(prototype)单元格向 TableView 添加页脚:

enter image description here

当实例化页脚时,dequeueReusableHeaderFooterViewWithIdentifier() 为单元格返回 nil,但 dequeueReusableCellWithIdentifier() 不是。我在 Interface Builder 中创建了一个带有正确标识符的页脚 View :

然后我实例化页脚:

func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let id = "MyFooter"
//    this works:
//    guard let footerCell = self.todaysChoresTableView.dequeueReusableCellWithIdentifier(id) else { 
//        return nil 
//    }
    guard let footerCell = self.todaysChoresTableView.dequeueReusableHeaderFooterViewWithIdentifier(id) else {
        // this doesn't, always returning nil
        return nil
    }
}

为什么 dequeueReusableHeaderFooterViewWithIdentifier() 总是返回 nil?

(lldb) po self.tableView.dequeueReusableHeaderFooterViewWithIdentifier(id)
nil

(lldb) po self.tableView.dequeueReusableCellWithIdentifier(id)
▿ Optional(<UITableViewCell: 0x7fa313c835f0; frame = (0 0; 600 44); clipsToBounds = YES; layer = <CALayer: 0x7fa313c73150>>)
  - Some : <UITableViewCell: 0x7fa313c835f0; frame = (0 0; 600 44); clipsToBounds = YES; layer = <CALayer: 0x7fa313c73150→

更新:

我也试过这样做,虽然现在它不再是 nil,但它似乎并没有真正根据我的 IB 设计构建 View :

tableView.registerClass(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: id)
let footerView: UITableViewHeaderFooterView? = self.todaysChoresTableView.dequeueReusableHeaderFooterViewWithIdentifier(id)
footerView!.backgroundColor = UIColor.redColor()
return footerView

最佳答案

您是否尝试过使用以下代码在代码中注册 NIB:

tableView.registerNib(UINib(nibName: "MyFooter", bundle: nil), forHeaderFooterViewReuseIdentifier: "MyFooterID")

关于ios - dequeueReusableHeaderFooterViewWithIdentifier 为单元格返回 nil,但 dequeueReusableCellWithIdentifier 不是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35541788/

相关文章:

ios - Swift 将 NSDate 转换为 24h 格式在设备上失败

ipad - UITableView背景颜色

Xcode/Swift - 多个 View Controller 中的元素

swift - firebase 错误(子 :) Must be a non-empty string and not contain '.' '#' '$' '[' or ']

macos - Swift 1.2 下标不可用

ios - 从不同的 UITableView 中取出 UITableViewCell

iphone - iOS——在单元格了解其大小后配置 UITableViewCell

ios - 如何在 Google Map iOS SDK 中处理多种类型的聚类

ios - 如何从 NSDictionary 分配图像和标签值?

iPhone如何实现一个 "Wide"的UITableViewCell?