I have a TableViewController below that I am trying to populate with a query request from Parse. The idea is that the call (which I have checked and is returning the necessary information) then fills the arrays, which I then use to populate the TableViewCells. These cells also have a custom class ('TableViewCell').
For some reason, 'self.tableView.reloadData()' is definitely causing the crash. When I remove it, it doesn't crash but the tableviewcells don't update with the parse information. Any ideas?
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
}
First of all, you are not checking cap , imdic , price by type. And reloading tableView many times in cycle. Replace
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()
}
with
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()
Also, don't dequeue cell that way. Replace
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
}
with
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
}
But I think that problem could always be inside TableViewCell class🤔 For example, captionLabel could be nil .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.