2016-02-27 56 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可能是

相关问题