我想在我的UITableView为空时显示图像背景。 目前我尝试添加一个UIImageView到我的视图控制器包含一个表,但XCode不允许它。iPhone UITableView图像背景当表空时
有没有一种很好的方法呢?
我想在我的UITableView为空时显示图像背景。 目前我尝试添加一个UIImageView到我的视图控制器包含一个表,但XCode不允许它。iPhone UITableView图像背景当表空时
有没有一种很好的方法呢?
您可以在表视图顶部添加图像视图或更改表视图的背景视图。
// Check if table view has any cells
int sections = [self.tableView numberOfSections];
BOOL hasRows = NO;
for (int i = 0; i < sections; i++) {
BOOL sectionHasRows = ([self.tableView numberOfRowsInSection:i] > 0) ? YES : NO;
if (sectionHasRows) {
hasRows = YES;
break;
}
}
if (sections == 0 || hasRows == NO)
{
UIImage *image = [UIImage imageNamed:@"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// Add image view on top of table view
[self.tableView addSubview:imageView];
// Set the background view of the table view
self.tableView.backgroundView = imageView;
}
UITableView
有一个backgroundView
属性。将此属性设置为包含背景图像的UIImageView
。
请注意,我不希望显示为UITableView的任何背景。我只想在表格没有显示单元时显示一些背景。我只是想隐藏表格并改为放置背景。你的建议会对此有用吗? – bashan 2012-01-18 21:37:15
在你numberOfRowsInSection方法:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if data.count > 0 {
return data.count
}
else{
let image = UIImage(named: "Nature")
let noDataImage = UIImageView(image: image)
noDataImage.frame = CGRect(x: 0, y: 0, width: tableView.bounds.width, height: tableView.frame.height)
tableView.backgroundView = noDataImage
tableView.separatorStyle = .none
return 0
}
}
numberOfSections应大于0
谢谢,不错的把戏。顺便说一句,我如何可视化地设计一个UIView并将其用作桌面上的图层?我不再有选择简单地使用新的XCode故事板来设计视图... – bashan 2012-01-18 21:46:04
您可以通过创建一个新的nib文件并将其加载到UIView中来设计视图。 – simonbs 2012-01-18 21:49:21
我曾经在旧的XCode上做过。故事板附带的新XCode似乎以不同的方式工作。它不允许您创建新的自定义大小视图,并且所有视图都位于同一个故事板文件中。 – bashan 2012-01-19 21:16:57