该应用程序在该行崩溃
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为零。这是一个自定义单元格,在故事板上创建了UITableView。任何人都可以告诉我可以尝试的解决方案吗?
答案 0 :(得分:2)
如果在调用addImagesValue
之前调用awakeFromNib
,那么您的代码将清空该数组。我不认为这就是你想要的。这是一个更好的解决方案:
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
}
}