在UITableViewController中处理空的UITableView [英] Handling empty UITableView in UITableViewController
问题描述
我有一个填充了一些数据的UITableViewController。如果数据返回空,显然表是空的。什么是适当的方法来处理这种情况,并提出像无数据可用的UILabel。
I have a UITableViewController that is populated with some data. If the data comes back empty, obviously the table is empty. What is the appropriate method to use to handle this case and put up something like a UILabel with "No Data Available".
我一直在使用 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
但事实证明它有点麻烦,我不再相信它是最好的地方。
I have been using - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
but it's proven a bit cumbersome and I'm not longer confident it's the best place to do this.
推荐答案
我只是在没有数据时隐藏了tableview。只需将UITableView子类化并覆盖reloadData。您还可以相应地显示/隐藏文本标签以显示无可用数据。
I just hid the tableview when there was no data instead. Simply subclass UITableView and override reloadData. You can also show/hide a text label appropriately to show no data available.
- (void)reloadData {
[super reloadData];
BOOL dataPresent = FALSE;
int sections = [self.dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)] ? [self.dataSource numberOfSectionsInTableView:self] : 1;
for (int i = 0; i < sections; i++) {
if ([self.dataSource tableView:self numberOfRowsInSection:i] > 0) {
dataPresent = TRUE;
break;
}
}
self.hidden = !dataPresent;
return;
}
这篇关于在UITableViewController中处理空的UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!