簡體   English   中英

為什么會出現錯誤:致命錯誤:解開Optional值時意外發現nil?

[英]Why am I getting the error: fatal error: unexpectedly found nil while unwrapping an Optional value?

我下面有一個TableViewController,我試圖用來自Parse的查詢請求填充。 這個想法是,該調用(我已經檢查並返回必要的信息)然后填充了數組,然后使用這些數組填充TableViewCells。 這些單元格還具有一個自定義類('TableViewCell')。

由於某些原因,'self.tableView.reloadData()'肯定會導致崩潰。 當我刪除它時,它不會崩潰,但tableviewcell不會使用解析信息進行更新。 有任何想法嗎?

 import UIKit
 import Parse

 class AuctionViewController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.registerClass(TableViewCell.self, forCellReuseIdentifier: "Cell")


} 

var capArray = [String]()
var imageDic = [String: [PFFile]]()
var priceArray = [Int]()


override func viewDidAppear(animated: Bool) {

    capArray.removeAll(keepCapacity: true)
    imageDic.removeAll(keepCapacity: true)
    priceArray.removeAll(keepCapacity: true)

    let query = PFQuery(className: "SellerObject")
    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

        if let objects = objects {
            for o in objects {
                if o.objectForKey("caption") != nil && o.objectForKey("imageFile") != nil && o.objectForKey("price") != nil {
                let cap = o.objectForKey("caption") as? String
                    self.capArray.append(cap!)
                let imdic = o.objectForKey("imageFile") as? [PFFile]
                self.imageDic[cap!] = imdic
                let price = o.objectForKey("price") as? String
                let priceInt = Int(price!)
                self.priceArray.append(priceInt!)
                print(self.capArray)
                print(self.imageDic)
                print(self.priceArray)

            }
                self.tableView.reloadData()
            }

        }

    }


}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return capArray.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell

    cell.captionLabel.text = self.capArray[indexPath.row]
    return cell
}

首先,您不是按類型檢查capimdicprice 並循環多次重載tableView 更換

for o in objects {
            if o.objectForKey("caption") != nil && o.objectForKey("imageFile") != nil && o.objectForKey("price") != nil {
            let cap = o.objectForKey("caption") as? String
                self.capArray.append(cap!)
            let imdic = o.objectForKey("imageFile") as? [PFFile]
            self.imageDic[cap!] = imdic
            let price = o.objectForKey("price") as? String
            let priceInt = Int(price!)
            self.priceArray.append(priceInt!)
            print(self.capArray)
            print(self.imageDic)
            print(self.priceArray)

        }
            self.tableView.reloadData()
        }

for o in objects {
           if let cap = o.objectForKey("caption") as? String,
           let imdic = o.objectForKey("imageFile") as? [PFFile],
           let priceInt = (o.objectForKey("price") as? String).flatMap({ Int($0))}) {
              self.capArray.append(cap)
              self.imageDic[cap] = imdic
              self.priceArray.append(priceInt)
              print(self.capArray)
              print(self.imageDic)
              print(self.priceArray)
           }
        }
self.tableView.reloadData()

另外,請勿以這種方式使單元出隊。 更換

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
      let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell

      cell.captionLabel.text = self.capArray[indexPath.row]
      return cell
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? TableViewCell else {
        assertionFailure("cell for index-path:\(indexPath) not found")
        return UITableViewCell()
    }

    cell.captionLabel.text = self.capArray[indexPath.row]
    return cell
}

但是我認為問題可能總是存在於TableViewCell類中。例如, captionLabel可以為nil

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM