reloadData()致命错误:在展开Optional值时意外发现nil

时间:2014-12-04 13:57:59

标签: ios uitableview swift xcode6 reloaddata

该应用程序在该行崩溃 enter image description here

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。任何人都可以告诉我可以尝试的解决方案吗?

1 个答案:

答案 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
    }

}