iphone - UITableView(Controller) - 隐藏空行

标签 iphone objective-c uitableview

我正在尝试创建一个 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/

相关文章:

ios - 复制 iOS 邮件撰写界面的行为,但 View 不跟随光标

ios - 动画 [tableView reloadData]

ios - 我如何编写iphone 3.5英寸屏幕和4英寸屏幕的代码

iphone - 如何将 AVAsset 旋转到纵向模式?

ios - 升级iOS应用程序

ios - UIDocumentInteractionController 禁止在某些应用程序中打开

iphone - NSTimer 选择器调用

iphone - iPhone SIGABRT崩溃0x00000000:帮助我进行多项选择

ios - 复制 block

ios - 如何防止 UITableView 分隔符颜色在 UIPopoverController (iOS7) 中变黑?