ios - 当 UITableView 为空时显示图像

标签 ios swift uitableview pfobject pftableviewcell

我试图在我的 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/

相关文章:

ios - UITableViewCell 背景图像在 iOS 8 中未缩放

objective-c - Objective-C : How to change text color in navigation bar

ios - 阅读 RSS 提要的安全问题和想法

ios - 在另一个 View Xcode 中显示 ViewController

ios - 如何使用自动布局垂直堆叠 TableView ?

ios - 一些不需要的 UIImageViews 在尝试将它们设置为 UITableView 的边框时消失

ios - 在 iphone 中格式化 UITableView 单元格

ios - 如何在 Swift 中使用查询语句检索数据?

ios - 以编程方式创建的 UITableView 中单元格的响应能力

swift - 如何初始化scrollView分页的起始索引?