回答
您希望返回一个报告缺少数据的单元。
如果你保持在一个阵列,这是一个类属性你的数据(假设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
}
您可以在-tableView:cellForRowAtIndexPath:实现中添加一个测试,并在没有数据时显示一个特殊的UITableViewCell,指出“无结果”。
通常看起来更好的另一种方法是直接向UITableView添加自定义视图。当有结果显示时,不要忘记将其删除。
你可以一个UILabel添加到您的视图 这可以通过编程来创建或者在你的xib中。如果您的厦门国际银行包含一个UITableView为[自我观点】,这是典型的一个UITableViewController,然后在你的viewController从库中创建标签的属性
@property (nonatomic, retain) IBOutlet UILabel *noResultsLabel;
拖动一个UILabel到文档窗口为同级你的“表格视图”。 设置文本属性(36pt粗体字,浅灰色看起来不错)
控制从“文件所有者”拖动来将插座连接到标签。
在您的服务器的回调,可以将标签添加到视图。这里有一个例子:
#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:如果一个适合你的设计更好
我正在考虑显示无论是页眉/页脚的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运作良好,并隐藏视图一旦有数据。
正如在下面的链接中提到的,如果我们创建一个标签并将其设置为下面的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个部分,则如果使用deleteRowsAtIndexPaths,则应用程序会崩溃。为了弥补这一点,我总是返回1的部分数量。如果数据存在,我还添加了'self.tableView.backgroundView = nil'。 – davew 2016-05-17 03:55:06
这里我使用如下代码:
-(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 ];
}
- 1. UITableView的屏幕截图
- 2. 发送数据到屏幕
- 3. 屏幕抓取数据库
- 4. UITableview所选行的屏幕截图
- 5. UITableView只占用屏幕的一部分
- 6. iOS6和屏幕外的UITextView在UITableView
- 7. UICollectionView或UITableView要超过屏幕尺寸
- 8. UITableView在大于屏幕的景观中
- 9. 调整iPhone 5的UITableview屏幕
- 10. UITableView tableFooterView iOS6以外的屏幕?
- 11. iOS UITableView细胞适合屏幕宽度
- 12. 如何从当前屏幕刷新UITableView
- 13. swift3 uitableView重载使屏幕闪烁
- 14. iOS - UITableView滚动条走出屏幕
- 15. 单个屏幕上的两个UITableView
- 16. 打印具有屏幕内容的UITableView?
- 17. 以编程方式填充的UITableView无法填充屏幕
- 18. 根据项目数量和屏幕比例分割屏幕
- 19. 从屏幕传递数据到屏幕xcode?
- 20. Uitableview无法保存数据
- 21. 在加载时关闭屏幕时,uitableview单元格显示错误的数据
- 22. 将数据从UITableView推送到同一屏幕上的另一个UIViewController
- 23. 屏幕捕获后无法访问缓冲区数据
- 24. 无效不更新屏幕
- 25. 无法添加屏幕
- 26. Sitecore:无法登录屏幕
- 27. 无效()不刷新屏幕
- 28. 无法捕获屏幕
- 29. Firebase Analytics上的屏幕跟踪数据
- 30. 屏幕抓取和SQL数据库
是缓存此细胞像其他普通的细胞是个好主意?如果是这样,你怎么做? – Yazzmi 2010-07-20 22:41:52
您不必担心缓存这个单元,因为它会在您实际显示数据的时刻消失,autorelease会为您处理它的内存。它永远不会从表格中滚动,所以真的没有什么需要缓存的。 顺便说一句,你也想这样做,你也加载你的数据。然后当你的网络过程完成并且你已经填充了你的数据源时,在你的UITableView上调用'reloadData'。 – 2010-07-21 02:02:24
就像一个魅力 - 只需要调整字体有点来得到我想要的消息:[cell.textLabel setFont:[UIFont fontWithName:@ Helvetica-Bold“size:13]]; – jaySF 2012-03-07 22:43:04