我正在尝试创建一个 UITableView
,它只显示其中包含数据内容的行数。这是一个菜单系统,假设在特定菜单中有 5 个选项,我只想在屏幕上显示 5 行,用背景图像代替空行(我想类似的例子是世界时钟)。我该怎么办?我四处搜寻,但找不到任何有用的东西。非常感谢。
---------------------
Menu Item 1
---------------------
Menu Item 2
---------------------
Rest of view as image
---------------------
使用分组 TableView 可行,但我真的不想使用它。
最佳答案
您的 UITableView 可以有一个 backgroundView。将它设置为 UIImageView,并将其框架设置为 TableView 的边界。此代码未经测试,但应该会给您一个想法。
UIImage * img = [UIImage imageNamed:@"myImage.jpg"];
UIImageView * imgView = [[UIImageView alloc] initWithImage:img];
imgView.frame = myTableView.bounds;
myTableView.backgroundView = imgView;
编辑: 上面的代码会产生一个类似世界时钟的背景。要使其作为一个单元格显示在其他单元格下方,请更改您的数据源:
- numberOfRowsInSection:section:
应该比以前多返回一个。
- tableView:canEditRowAtIndexPath:
和 tableView:canMoveRowAtIndexPath:
应该为这个新单元格返回 NO
。
- (UITableViewCell *)tableView:cellForRowAtIndexPath:
需要在请求时创建新单元格,因此如果您有 5 个数据单元格,第 6 个将返回:
UITableViewCell * cell = //create your cell
//change the height to match the remaining space
UIImage * img = [UIImage imageNamed:@"myImage.jpg"];
UIImageView * imgView = [[UIImageView alloc] initWithImage:img];
cell.backgroundView = imgView;
我不确定您是否必须设置 ImageView 大小,但您必须设置单元格大小以占用剩余空间,在您的表格 View 委托(delegate) tableView:heightForRowAtIndexPath:
最后一行:
//if final row
return myTableView.bounds.size.height - 16.0 * myData.count; //modify from the default 16.0 as needed.
关于iphone - UITableView(Controller) - 隐藏空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3672057/