2010-07-20 23 views
8

我的UITableView從互聯網上獲取數據。有時它不會收到任何(單元格)數據。它只是顯示一張空白表。UITableView無數據屏幕

如何向用戶顯示沒有找到數據記錄的消息/單元?

回答

25

您希望返回一個報告缺少數據的單元。

如果你保持在一個陣列,這是一個類屬性你的數據(假設NSArray *listings),你可以去:

-(NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section 
{ 
    if ([self.listings count] == 0) { 
     return 1; // a single cell to report no data 
    } 
    return [self.listings count]; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([self.listings count] == 0) { 
     UITableViewCell *cell = [[[UITableViewCell alloc] init] autorelease]; 
     cell.textLabel.text = @"No records to display"; 
     //whatever else to configure your one cell you're going to return 
     return cell; 
    } 

    // go on about your business, you have listings to display 
} 
+0

是緩存此細胞像其他普通的細胞是個好主意?如果是這樣,你怎麼做? – Yazzmi 2010-07-20 22:41:52

+0

您不必擔心緩存這個單元,因爲它會在您實際顯示數據的時刻消失,autorelease會爲您處理它的內存。它永遠不會從表格中滾動,所以真的沒有什麼需要緩存的。 順便說一句,你也想這樣做,你也加載你的數據。然後當你的網絡過程完成並且你已經填充了你的數據源時,在你的UITableView上調用'reloadData'。 – 2010-07-21 02:02:24

+0

就像一個魅力 - 只需要調整字體有點來得到我想要的消息:[cell.textLabel setFont:[UIFont fontWithName:@ Helvetica-Bold「size:13]]; – jaySF 2012-03-07 22:43:04

2

您可以在-tableView:cellForRowAtIndexPath:實現中添加一個測試,並在沒有數據時顯示一個特殊的UITableViewCell,指出「無結果」。

通常看起來更好的另一種方法是直接向UITableView添加自定義視圖。當有結果顯示時,不要忘記將其刪除。

2

你可以一個UILabel添加到您的視圖 這可以通過編程來創建或者在你的xib中。如果您的廈門國際銀行包含一個UITableView爲[自我觀點】,這是典型的一個UITableViewController,然後在你的viewController從庫中創建標籤的屬性

@property (nonatomic, retain) IBOutlet UILabel  *noResultsLabel; 

拖動一個UILabel到文檔窗口爲同級你的「表格視圖」。 設置文本屬性(36pt粗體字,淺灰色看起來不錯)
控制從「文件所有者」拖動來將插座連接到標籤。

UITableView with no results UILabel

在您的服務器的回調,可以將標籤添加到視圖。這裏有一個例子:

#pragma mark - 
#pragma mark Server callbacks 
- (void)serviceExecutionComplete:(NSMutableArray *)results { 
    [self setServerData:results]; 
    if ([results count] == 0) 
    { 
     // no results, 
     CGRect viewFrame = [[self view] frame]; 
     [[self noResultsLabel] setFrame:CGRectMake(0, (viewFrame.size.height - 100)/2, viewFrame.size.width, 50.0)]; 
     [[self view] addSubview:[self noResultsLabel]]; 
    } 
    else 
    { 
      [[self noResultsLabel] removeFromSuperview]; 
     [self.tableView reloadData]; 
    } 
} 

你可以做的tableView同樣的事情:numberOfRowsInSection:如果一個適合你的設計更好

1

我正在考慮顯示無論是頁眉/頁腳的emptyDataView,或黑客行之一就像丹的建議答案一樣。

但我認爲最乾淨的是在tableView外創建一個視圖。

@property (weak, nonatomic) IBOutlet UIView *emptyDataView; 

然後隱藏/顯示numberOfRowsInSection:的觀點是這樣的:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    id sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section]; 

    // Show empty data view if no data 
    _emptyDataView.hidden = ([sectionInfo numberOfObjects] == 0) ? NO : YES; 

    return [sectionInfo numberOfObjects]; 
} 

這與核心數據NSFetchedResultsController運作良好,並隱藏視圖一旦有數據。

1

正如在下面的鏈接中提到的,如果我們創建一個標籤並將其設置爲下面的UITableView的背景視圖,那很容易。可能對某人有用。 http://www.appcoda.com/pull-to-refresh-uitableview-empty/

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    if (data) { 
     self.tableView.backgroundView = nil; 
     self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 
     return 1; 

    } else { 

     // Display a message when the table is empty 
     UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)]; 

     messageLabel.text = @"No data is currently available. Please pull down to refresh."; 
     messageLabel.textColor = [UIColor blackColor]; 
     messageLabel.numberOfLines = 0; 
     messageLabel.textAlignment = NSTextAlignmentCenter; 
     messageLabel.font = [UIFont fontWithName:@"Palatino-Italic" size:20]; 
     [messageLabel sizeToFit]; 

     self.tableView.backgroundView = messageLabel; 
     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 

    } 

    return 0; 
} 
+0

我試過這種方法。請注意,如果您在沒有數據時返回0個部分,則如果使用deleteRowsAtIndexPaths,則應用程序會崩潰。爲了彌補這一點,我總是返回1的部分數量。如果數據存在,我還添加了'self.tableView.backgroundView = nil'。 – davew 2016-05-17 03:55:06

-1

這裏我使用如下代碼:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
NSInteger numOfSections = 0; 
if ([jsonArray count] != 0){ 
    tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 
    numOfSections     = 1; 
    tableview.backgroundView = nil; 
} 
else{ 
    UILabel *noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableview.bounds.size.width, tableview.bounds.size.height)]; 
    noDataLabel.text    = @"No item found."; 
    noDataLabel.textColor  = [UIColor blackColor]; 
    noDataLabel.textAlignment = NSTextAlignmentCenter; 
    tableview.backgroundView  = noDataLabel; 
    tableview.separatorStyle  = UITableViewCellSeparatorStyleNone; 
} 
    return numOfSections; 
} 

- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section{ 
    return [jsonArray count ]; 
}