我正在使用一个自定义 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 文件和 swift 文件,重新创建它并将其作为单元格而不是页脚插入,这似乎有效。我在为其创建 swift 类文件之前已经创建了 Xib 文件,并且它们可能没有正确配置。
关于ios - TableView FooterView 找不到注册的 Nib ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39935473/