Ios 如何为UITableView正确配置空视图?
我需要设置一个视图,填充Ios 如何为UITableView正确配置空视图?,ios,objective-c,uitableview,xcode-storyboard,Ios,Objective C,Uitableview,Xcode Storyboard,我需要设置一个视图,填充UITableView的内容区域,并在该表视图为空时显示,否则隐藏。该视图包含图像、标签和执行简单操作的按钮。我希望实现两件事: 1)利用UITableView的backgroundView属性,以及 2)尽可能利用情节提要,同时最小化代码量(可能使用容器视图?) 然而,我不知道什么是最好的方式去做这件事。非常感谢您的见解,尤其是如果您自己已经实现了此解决方案。您可能可以通过多种方法来实现这一点,但最简单(概念上)的方法似乎是只使用具有两个子视图的容器视图: 非空时可见
UITableView
的内容区域,并在该表视图为空时显示,否则隐藏。该视图包含图像、标签和执行简单操作的按钮。我希望实现两件事:
1)利用UITableView
的backgroundView
属性,以及
2)尽可能利用情节提要
,同时最小化代码量(可能使用容器视图?)
然而,我不知道什么是最好的方式去做这件事。非常感谢您的见解,尤其是如果您自己已经实现了此解决方案。您可能可以通过多种方法来实现这一点,但最简单(概念上)的方法似乎是只使用具有两个子视图的容器视图:
- 非空时可见的
李>UITableView
- 保存图像/标签/按钮的
,当ui视图
为空UITableView
UITableView
即使您只使用
UITableView
并将其设置为backgroundView
以在UITableView
为空时显示UIView
,也必须完成几乎相同的工作量(您仍然必须创建两个视图)。您可能可以通过多种方法来完成此操作,但最简单(概念上)似乎只是使用具有两个子视图的容器视图:
- 非空时可见的
李>UITableView
- 保存图像/标签/按钮的
,当ui视图
为空UITableView
UITableView
即使仅使用
UITableView
并将其backgroundView
设置为UITableView
以在UITableView
为空时显示,也必须完成几乎相同的工作量(您仍然必须创建两个视图).首先,您要在viewdiload
中设置背景视图:
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen]bounds].size.width, [[UIScreen mainScreen]bounds].size.height)];
//Make this clear and it will be the same as the tableView background color
[backgroundView setBackgroundColor:[UIColor clearColor]];
//We can add anything to this text of images
[backgroundView addSubview:imageView];
//This is the important part
[self.tableView setBackgroundView:backgroundView];
现在要隐藏或查看背景视图,您需要在-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
中添加一些逻辑,例如:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//Or wherever your data is
if([self.data count]>0) {
self.tableView.backgroundView.hidden=YES;
return 1;
}
else {
self.tableView.backgroundView.hidden=NO;
}
return 0;
}
只需检查有多少节,如果表中没有节,则显示视图。如果有分区,则将其隐藏
编辑,显然,如果您愿意,也可以在界面生成器中设置UIView。确保尺寸正确。使用以下内容加载if:
UIView *view = [[[NSBundle mainBundle]
loadNibNamed:@"MyView"
owner:self options:nil]
firstObject];
您需要再次设置它:
[self.tableView setBackgroundView:view]代码>首先要在视图加载中设置背景视图:
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen]bounds].size.width, [[UIScreen mainScreen]bounds].size.height)];
//Make this clear and it will be the same as the tableView background color
[backgroundView setBackgroundColor:[UIColor clearColor]];
//We can add anything to this text of images
[backgroundView addSubview:imageView];
//This is the important part
[self.tableView setBackgroundView:backgroundView];
现在要隐藏或查看背景视图,您需要在-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
中添加一些逻辑,例如:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//Or wherever your data is
if([self.data count]>0) {
self.tableView.backgroundView.hidden=YES;
return 1;
}
else {
self.tableView.backgroundView.hidden=NO;
}
return 0;
}
只需检查有多少节,如果表中没有节,则显示视图。如果有分区,则将其隐藏
编辑,显然,如果您愿意,也可以在界面生成器中设置UIView。确保尺寸正确。使用以下内容加载if:
UIView *view = [[[NSBundle mainBundle]
loadNibNamed:@"MyView"
owner:self options:nil]
firstObject];
您需要再次设置它:[self.tableView setBackgroundView:view]代码>