在UITableViewController中处理空的UITableView

Ter*_*ary 7 iphone uitableview

我有一个UITableViewController,填充了一些数据.如果数据返回空,显然表是空的.什么是适当的方法来处理这种情况,并提出像"没有数据可用"的UILabel.

我一直在使用,- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section但事实证明它有点麻烦,我不再相信它是最好的地方.

cra*_*erm 5

我可能会稍微改变一下哲学,只在内容可用时显示UITableView,并让UIView在内容准备好之前显示"无内容"可用图像.

一旦获取了内容并准备好呈现 - 通过"无内容"图像动画或创建UITableView到视图层次结构中,并要求它重新加载其数据.这将使它开始通过它的数据源委托回调.

至少通过这种方式,您不必担心显示没有数据的tableview,并将问题与UITableViewDataSource回调方法混合在一起.


Sha*_*awn 1

当没有数据时,我只是隐藏了表格视图。只需子类化 UITableView 并覆盖 reloadData。您还可以适当地显示/隐藏文本标签以显示没有可用数据。

- (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;
}
Run Code Online (Sandbox Code Playgroud)