2010-07-20 15 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 ]; 
}