为什么我收到错误:致命错误:在解包可选值时意外发现nil?

时间:2016-02-27 18:37:23

标签: ios swift uitableview parse-platform pfquery

我在下面有一个TableViewController,我试图用Parse的查询请求填充。我的想法是,调用(我已经检查并返回必要的信息)然后填充数组,然后我用它来填充TableViewCells。这些单元格还有一个自定义类('TableViewCell')。

出于某种原因,'self.tableView.reloadData()'肯定会导致崩溃。当我删除它时,它不会崩溃,但tableviewcells不会使用解析信息更新。有什么想法吗?

 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
}

1 个答案:

答案 0 :(得分:0)

首先,您不是按类型检查上限 imdic 价格。并在循环中多次重新加载 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