我目前正在开发的iOS应用是基于tabbar的,其中一个选项卡是UITableViewController。
问题是,当我打开这个选项卡时,如果数据源为空(无论出于什么原因),我希望显示另一个视图,里面有一些消息/图片,而不是只有tableviewcontroller中的空白视图。
我尝试了类似以下的代码:
- (void)viewWillAppear:(BOOL)animated {
if ([myData count] == 0) {
if (!emptyView) {
emptyView = [[UIView alloc] initWithFrame:self.view.frame];
UILabel *emptyMsg = [[UILabel alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 2, self.view.frame.size.width, 20)];
emptyMsg.text = @"This is Empty !";
emptyMsg.textAlignment = UITextAlignmentCenter;
[emptyView addSubview:emptyMsg];
}
[self.view insertSubview:emptyView atIndex:0];
}
else {
if (emptyView != nil) { [emptyView removeFromSuperview]; emptyView = nil; }
[self.tableView reloadData];
[super viewWillAppear:animated];
}
}
在视图控制器中,将emptyView定义为一个iVar。
但是它没有按预期工作,我找不到原因 :/
你们能否看一下,并给我正确的方法来实现这种行为呢?
谢谢!