2015-06-16 9 views
0

我需要設置一個填充UITableView的內容區域的視圖,並且每當該表視圖爲空時顯示該視圖,否則將隱藏該視圖。該視圖包含圖像,標籤和執行簡單操作的按鈕。我希望能實現兩件事情:如何正確配置UITableView的空視圖?

1)槓桿backgroundView財產UITableView,並

2)槓桿storyboard儘可能同時減少代碼量(也許使用的容器視圖?)。

但是,我不確定什麼是最好的方式去做這件事。深入瞭解,特別是如果您已經自己實施了此解決方案。

回答

0

你也許可以做到這一點了一堆的方式,但最簡單的(概念),似乎只是使用具有兩個子視圖容器視圖:

  • 一個UITableView是可見時,它不是空的。
  • A UIView保存您的圖像/標籤/按鈕,當 UITableView爲空時可見。

根據的UITableView

內容簡單地改變它們的可見度,即使你只使用UITableView,並設置它的backgroundViewUIView時顯示UITableView是空的,幾乎相同的工作數量必須完成(您仍然必須創建兩個視圖)。

0

首先要設置在viewDidLoad你的背景圖:

UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen]bounds].size.width, [[UIScreen mainScreen]bounds].size.height)]; 
//Make this clear and it will be the same as the tableView background color 
[backgroundView setBackgroundColor:[UIColor clearColor]]; 
//We can add anything to this text of images 
[backgroundView addSubview:imageView]; 
//This is the important part 
[self.tableView setBackgroundView:backgroundView]; 

我們隱藏或查看後臺查看您需要一點邏輯添加到- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView例如:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    //Or wherever your data is 
    if([self.data count]>0) { 

     self.tableView.backgroundView.hidden=YES; 
     return 1; 

    } 

    else { 

     self.tableView.backgroundView.hidden=NO; 


    } 


    return 0; 
} 

只需檢查您有多少部分,如果表中沒有部分顯示視圖。如果你有部分,然後隱藏它。

編輯,顯然如果你願意,你也可以在界面生成器中設置一個UIView。確保它是正確的尺寸。如果負載喜歡的東西:

UIView *view = [[[NSBundle mainBundle] 
     loadNibNamed:@"MyView" 
     owner:self options:nil] 
     firstObject]; 

,並再次需要設置它:[self.tableView setBackgroundView:view];