我试图在我的 UITableView 为空时显示图像,但由于某些原因,代码不会在 tableView 为空时运行,尽管在有单元格时它没问题:
// 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
}
我试过 self.objects?.count == 0
,我试过使用 numberOfRowsInSection
,我试过 visibleCells
.
我错过了什么?
最佳答案
如果数据源中没有对象,则不会调用上面的函数。因此,您需要找到另一个地方来做。如果你的数据源是静态的(没有删除或添加操作),我建议在 viewDidLoad
中检查数据源是否为空。如果可以更改数据源,那么我建议在从数据源中删除对象的函数中进行更改。每次删除对象时,都会检查数据源,如果它变空则显示图像。
关于ios - 当 UITableView 为空时显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31196409/