Iphone 在UITableViewController中处理空UITableView

Iphone 在UITableViewController中处理空UITableView,iphone,uitableview,Iphone,Uitableview,我有一个UITableViewController,其中填充了一些数据。如果数据返回为空,则表显然为空。处理这种情况的适当方法是什么,并在UILabel上显示“无可用数据”之类的内容 我一直在使用-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section,但它被证明有点麻烦,我不再相信它是最好的地方。我可能会稍微颠倒一下理念,只在内容可用时显示UITableView,并让UIVi

我有一个UITableViewController,其中填充了一些数据。如果数据返回为空,则表显然为空。处理这种情况的适当方法是什么,并在UILabel上显示“无可用数据”之类的内容


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

我可能会稍微颠倒一下理念,只在内容可用时显示UITableView,并让UIView显示“无内容”可用图像,直到内容准备就绪

获取内容并准备好显示后,在“无内容”图像上设置UITableView的动画或将其创建到视图层次结构中,并要求其重新加载数据。这将使它通过数据源委托回调开始工作


至少通过这种方式,您不必担心显示没有数据的tableview,也不必担心会与UITableViewDataSource回调方法混淆。

为什么不在表为空时将UITableView的backgroundView属性更改为适当的属性?您可以随时为tableView加载或重新加载数据来更新backgroundView属性。backgroundView的大小会自动调整为tableView的大小,您可以对其进行自定义,使其具有空箱子所需的任何图片、文本和控件。

我只是在没有数据时隐藏了tableView。只需将UITableView子类化并覆盖重载数据。您还可以适当地显示/隐藏文本标签,以不显示任何可用数据

- (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;
}
-(void)重新加载数据{
[超级重载数据];
BOOL dataPresent=FALSE;
int sections=[self.dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)]?[self.dataSource numberOfSectionsInTableView:self]:1;
对于(int i=0;i0){
dataPresent=TRUE;
打破
}
}
self.hidden=!dataPresent;
返回;
}

我喜欢这样。因此,我开始使用基于导航的应用程序,最后使用了UITableViewController。所以,如果我隐藏了tableView,我该如何向视图中添加内容,以及下面的视图是什么?当我打开InterfaceBuilder时,我只看到tableview。这让我很困惑。