2015-06-01 39 views
0

我檢查瞭如何使背景UITableView與此代碼在cellForRowAtIndexPath的iOS tableView.backgroundView無細胞

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

它工作正常,但在沒有電池時,背景不顯示因爲cellForRowAtIndexPath在沒有單元時沒有被調用。

好吧,我想顯示背景,即使沒有電池,所以我嘗試:

- (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,[email protected]等?

乾杯!

回答

1

這是我走近它:

- (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]]; 
} 
+0

不錯的人,它的工作原理,但你知道這個問題的第二部分?如何使用不同設備(iPhone4/5/6/6 +)製作不同的背景大小? – Viny76

+1

我還沒有嘗試過,但我相信你可以從資產目錄中做到這一點,它會爲設備選擇特定的圖像。我確定在SO上有答案。 –