I'm trying to display an image when my UITableView is empty, but for some reason the code won't run when the tableView is empty, though it's fine when there are cells:
// Configures cell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject!) -> (PFTableViewCell!) {
let cell = tableView.dequeueReusableCellWithIdentifier("upcomingCell", forIndexPath: indexPath) as! UpcomingTVCell
cell.configureCell(object)
//makes it so the separators won't dissapear on us
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
if (self.objects?.count == nil) {
println("test")
UIGraphicsBeginImageContext(self.view.frame.size);
UIImage(named: "homeZero")?.drawInRect(self.view.bounds)
var backImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
self.tableView.backgroundColor = UIColor(patternImage: backImage)
} else {
self.tableView.backgroundColor = UIColor.whiteColor()
println("this other code ran")
}
return cell
}
I've tried self.objects?.count == 0
, I've tried using numberOfRowsInSection
, and I've tried visibleCells
.
What am I missing?
If there is no objects in the datasource, the function above will not be called. Hence you need to find another place to do it. If your datasource is static (no delete or add operations), I suggest to check if the datasource is empty in viewDidLoad
. If the datasource could be changed, then I suggest to do it in the function where you delete the object from datasource. Every time you delete an object, you check the datasource, if it becomes empty then show the image.