2016-02-27 54 views
0

我有一個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 
} 
+0

考慮使用自定義結構或類而不是三個單獨的數組。 – vadian

+0

你有沒有設置self.tableView.datasource? –

+0

哪個變量是零?查看調試器。 – Darko

回答

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可能是

相關問題