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
}
考慮使用自定義結構或類而不是三個單獨的數組。 – vadian
你有沒有設置self.tableView.datasource? –
哪個變量是零?查看調試器。 – Darko