本文介绍了reloadData()致命错误:展开可选值时意外发现nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
该应用程序在行崩溃
The app crashes at the line
class ImageValueCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var imagesList: UITableView!
var imageArray: NSArray!
override func awakeFromNib() {
//super.awakeFromNib()
// Initialization code
imagesList.delegate = self;
imagesList.dataSource = self;
imageArray = NSArray()
imagesList.reloadData()
}
func addImagesValue(objectList: NSMutableArray, machine: WIFIMachine){
imageArray = objectList
imagesList.reloadData() //CRASHED HERE
}
}
我进行跟踪,发现崩溃发生时imageList为nil.这是一个在情节提要板上创建的带有UITableView的自定义单元格.有人可以建议我尝试的可能解决方案吗?
I trace through and found that imageList is nil when the crash happens. This is a custom cell with a UITableView created on the storyboard. Can anyone advise me on the possible solution that i could try?
推荐答案
如果在调用awakeFromNib
之前调用addImagesValue
,则您的代码将清空该数组.我认为那不是你想要的.这是一个更好的解决方案:
If you are calling addImagesValue
before the awakeFromNib
is called, then your code will empty the array. I don't think that is what you want. Here is a better solution:
class ImageValueCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var imagesList: UITableView!
var imageArray: NSArray = NSArray() {
didSet {
// whenever the imageArray changes, reload the imagesList
if let imagesList = imagesList {
imagesList.reloadData()
}
}
}
override func awakeFromNib() {
// why isn't the below done from inside the nib file? That's how I would do it.
imagesList.delegate = self
imagesList.dataSource = self
imagesList.reloadData()
}
func addImagesValue(objectList: NSMutableArray, machine: WIFIMachine){
imageArray = objectList
}
}
这篇关于reloadData()致命错误:展开可选值时意外发现nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!