objective-c - 如果表格 View 为空则显示标签

标签 objective-c ios uitableview core-data

我正在使用 Core Data 存储项目名称并将其显示在表格 View 中。当 TableView 为空(数据库中没有数据)时,它是空白的。从用户的角度来看,这不是很好,所以我希望能够显示一个标签,上面写着“没有项目”。

我该怎么做?我需要:

  1. 检查数据库是否为空并设置一个BOOL
  2. 如果此 BOOL 设置为 true 或 YES,是否显示标签?或将 cell.textLabel.text 设置为“无项目”

如果我走在正确的轨道上,我会非常感谢一些示例代码来插入我朝着正确的方向前进。

谢谢

最佳答案

我最终使用以下代码检查我的 Core Data 数据库是否为空。工作出色。这必须放在 CoreDataController.m 文件中。

NSLog(@"Total number of rows = %d ", totalNumberOfRowsInDatabase);

    if (totalNumberOfRowsInDatabase == 0)
    {

        NSLog(@"Database is empty");
        UIImage *image = [UIImage imageNamed:@"emptyTable.png"];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        [imageView setFrame:self.tableView.bounds];
        [self.tableView setBackgroundView:imageView];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.tableView setBackgroundColor:[UIColor clearColor]];

    }
    else 
    {
        [self.tableView setBackgroundView:nil];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        [self.tableView setBackgroundColor:[UIColor whiteColor]];
    }

    return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];

关于objective-c - 如果表格 View 为空则显示标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9458301/

相关文章:

ios - CQ5 和 Hybris 在电子商务中的作用

ios - 我遇到这样的错误,我想到了 tableView,但我在堆栈跟踪中看到了有关 FBSDK 的信息

ios - 在 swift xcode 中通过 View 发送数据

objective-c - Objective C 代码是否有 github markdown 语言标识符?

iphone - 调试 NSNotificationCenter 问题的方法?

ios - 由于 'GET' 请求多个对象的内存问题导致应用程序崩溃

ios - 标签框未正确停留在 `UICollectionViewCell`

objective-c - NSScreen的可见框太大

ios - 无法选择表格 View 的单元格

ios - 'UITableViewCell' 没有名为 'nameLabel' 的成员