iOS tableView.backgroundView 没有单元格

iOS tableView.backgroundView with no cell

我已经在 cellForRowAtIndexPath 中检查了如何使用 UITableView 制作背景:

UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.JPG"]];
[tempImageView setFrame:tableView.frame];
tableView.backgroundView = tempImageView;

可以正常使用,但是当没有cell时,背景不显示,因为cellForRowAtIndexPath没有cell时不调用

好的,即使没有单元格我也想显示背景,所以我试了:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.events.count == 0)
    {
        UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.JPG"]];
        [tempImageView setFrame:tableView.frame];
        //
        tableView.backgroundView = tempImageView;
    }
    return self.events.count;
}

但我不知道这是不是最好的选择?

以及如何在不同的设备上设置不同的背景尺寸 (iPhone4/5/6/6+)?仅使用 image.jpg、image@2x.jpg 等 ?

干杯!

我是这样处理的:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.backgroundView = [[UIImageView alloc] initWithImage:
                                     [UIImage imageNamed:@"Background0"]];
    [self.tableView.backgroundView setContentMode:UIViewContentModeScaleAspectFill];

    // Adding a table footer will hide the empty row separators

    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cell setBackgroundColor:[UIColor clearColor]];
}