UITableView No Data Screen
我的 UITableView 从互联网上获取数据。有时它不接收任何(单元格)数据。它只是显示一个空白表。
如何向用户显示未找到数据记录的消息/单元格?
您希望返回一个报告缺少数据的单元格。
如果您将单元格数据保存在一个类属性的数组中(比如说 NSArray *listings),您可以:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
–(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 // go on about your business, you have listings to display |
- 像其他常规单元格一样缓存这个单元格是个好主意吗?如果是这样,你是怎么做到的?
- 您不必担心缓存这个单个单元格,因为它会在您显示实际数据的那一刻消失,并且自动释放会为您处理它的内存。它永远不会滚出表格,所以真的没有什么可以缓存的。顺便说一句,您也想在加载数据时执行此操作。然后当你的网络进程完成并且你已经填充了你的数据源时,在你的 UITableView 上调用 reloadData。
- 像魅力一样工作 – 只需稍微调整字体即可获得我想要适合的消息:[cell.textLabel setFont:[UIFont fontWithName:@Helvetica-Bold” size:13]];
- 同样通常在这种情况下,您将希望使此单元格不可选择,或者您的应用程序可能会抛出错误的执行,除非您以其他方式处理它。将 selectionStyle 属性设置为 UITableViewCellSelectionStyleNone – stackoverflow.com/a/812969/962904
- 但是,如果我们使用 insertRowsAtIndexPaths 或 deleteRowsAtIndexPaths 行方法,这种方法会破坏应用程序。添加第一个ou删除最后一个单元格会报错,因为numberOfRowsInSection方法返回1,但是数据源数组为空。
- @Warthog – UITableView 仅通过其 dataSource 委托告诉它的方式知道数据源数组中的内容。这就是为什么即使真正的答案是 0,您也可以捏造 numberOfRowsInSection 始终回答 1。继续从源数组中删除最后一项;我们将声称有 1 行,然后将其填充为空,就像我们应该做的那样。
- 我建议为此目的使用表头。这似乎是一个黑客。如果您遵循这一点,则需要将代码撒在各处以处理”one”行的特殊情况 – 例如选择、删除等
您可以在视图中添加 UILabel
这可以通过编程方式或在您的 xib 中创建。如果您的 xib 包含作为 [self view] 的 UITableView,这对于 UITableViewController 来说很典型,那么为您的 viewController
上的标签创建一个属性
1
|
@property (nonatomic, retain) IBOutlet UILabel *noResultsLabel;
|
将 UILabel 从库中拖到文档窗口中,作为”表格视图”的兄弟。
设置文本属性(36pt粗体,浅灰色看起来还不错)
控制从”文件的所有者”拖动以将outlets连接到标签。
在您的服务器回调中,您可以将标签添加到视图。这是一个例子:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#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: 中做同样的事情,如果这更适合你的设计
您可以在 -tableView:cellForRowAtIndexPath: 实现中添加一个测试,并在没有数据时显示一个特殊的 UITableViewCell 说”没有可用的结果”。
另一种通常看起来更好的方法是直接向 UITableView 添加自定义视图。当有结果要显示时不要忘记删除它。
如以下链接中所述,如果我们创建一个标签并将其设置为 UITableView 的背景视图,如下所示。可能对某人有用。 http://www.appcoda.com/pull-to-refresh-uitableview-empty/
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
– (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 messageLabel.text = @“No data is currently available. Please pull down to refresh.”; self.tableView.backgroundView = messageLabel; } return 0; |
- 我试过这种方法。请注意,如果您在没有数据的情况下返回 0 个部分,则在使用 deleteRowsAtIndexPaths 时应用程序会崩溃。为了解决这个问题,我总是返回 1 作为节数。如果数据存在,我还添加了 self.tableView.backgroundView = nil 。
我正在考虑在页眉/页脚中显示 emptyDataView,或者像 Dan 建议的答案那样修改其中一行。
但我认为最干净的方法是在 tableView 之外创建一个视图。
1
|
@property (weak, nonatomic) IBOutlet UIView *emptyDataView;
|
然后像这样在 numberOfRowsInSection: 中隐藏/显示视图:
1
2 3 4 5 6 7 8 9 |
– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ id sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section]; // Show empty data view if no data return [sectionInfo numberOfObjects]; |
这适用于 Core Data NSFetchedResultsController,一旦有数据就隐藏视图。
这里我使用如下代码:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
–(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{ |
来源:https://www.codenong.com/3287722/