回答
您希望返回一個報告缺少數據的單元。
如果你保持在一個陣列,這是一個類屬性你的數據(假設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