ios - TableView FooterView 找不到注册的 Nib

标签 ios swift uitableview xib

我正在使用一个自定义 xib,其中包含一些我想要加载到 UITableView 页脚中的按钮和标签。 但是,当它尝试转到应用程序中的表时出现错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

表格 View 还有 5 个其他动态单元格,它们是在以下位置创建的:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

我不明白发生了什么。 我已经在 viewDidLoad 中注册了一个页脚 Nib 。

   override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Proxima Nova", size: 17)!]
    self.navigationController?.navigationBar.barTintColor = UIColor(red: 79/255, green: 116/255, blue: 136/255, alpha: 1.0)

    // create a longPressRecognizer that is used for bringing up the modal to select recipes and marking the meal as eaten
    let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(MealMapTableViewController.longPress(_:)))
    self.view.addGestureRecognizer(longPressRecognizer)
    navigationBar.title! = "Meal Map"

    // This allows for the side menu to appear from within the app
    if self.revealViewController() != nil {
        menuButton.target = self.revealViewController()
        menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
    }

    let footerNib = UINib(nibName: "WaterTrackerTableViewCell", bundle: nil)
    tableView.registerNib(footerNib, forCellReuseIdentifier: "WaterTrackerTableViewCell")
  }

然后我重写了创建页脚的方法。

// MARK: - Section Footer Methods
override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

    let footer = self.tableView.dequeueReusableHeaderFooterViewWithIdentifier("WaterTrackerTableViewCell") as! WaterTrackerTableViewCell
    footer.amountLeftLabel.text = "testing"

    return footer
}

WaterTrackerTableViewCell 定义如下:

class WaterTrackerTableViewCell: UITableViewHeaderFooterView {
    @IBOutlet var bottleButton8Oz: UIButton!
    @IBOutlet var bottleButton16Oz: UIButton!
    @IBOutlet var bottleButton24Oz: UIButton!
    @IBOutlet var bottleButton32Oz: UIButton!

    @IBOutlet var amountLeftLabel: UILabel!
    @IBOutlet var goalLabel: UILabel!

    @IBOutlet var waterProgressBar: UIProgressView!
}

如果我尝试包含此方法:

override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 90.0
}

它会使应用程序崩溃,但如果我排除该方法,则页脚不会出现。我在没有 xib 文件的情况下尝试了它

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
    footerView.backgroundColor = UIColor.blackColor()

    return footerView
}

override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 40.0
}

这样就可以了。

更新错误: Image of the Error

更新2: 我修复了 viewDidLoad 中所做的事情 来自

tableView.registerNib(footerNib, forCellReuseIdentifier: "WaterTrackerTableViewCell")

tableView.registerNib(footerNib, forHeaderFooterViewReuseIdentifier: "WaterTrackerTableViewCell")

但现在它仍然崩溃,我收到另一个错误:

2016-10-08 11:08:07.321 Cleanse App[3977:2893987] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (loaded)' with name 'WaterTrackerTableViewCell''

更新3:

Xib WaterTrackerTableViewCell and file name

最佳答案

我最终删除了 Xib 文件和 swift 文件,重新创建它并将其作为单元格而不是页脚插入,这似乎有效。我在为其创建 swift 类文件之前已经创建了 Xib 文件,并且它们可能没有正确配置。

关于ios - TableView FooterView 找不到注册的 Nib ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39935473/

相关文章:

iphone - 在 UIScrollView 上拖动图像

ios - Swift Cocoa Touch 自定义单元格为 IBOutlets 返回 nil

ios - Swift 本地通知已触发 - 在应用程序中执行某些操作

ios - SpriteKit 中的可弯曲 ARM 旋转 - 在点旋转

ios - Sprite Kit Swift 中的碰撞检测

iphone - 如何通过Uitableview的各个部分获取下一个类?

ios - 带有 c/c++ 模块错误的 Android Studio KMM 项目

ios - 在主数组的数组中查找 NSString

ios - 更新 UITableView 中的 UIImage

ios - 触发 UITapGesture 时从单元格的 subview 中获取单元格的引用