UITableView(Controller) - 隐藏空行

Ash*_*Bye 6 iphone objective-c uitableview

我正在尝试创建一个UITableView只显示其中包含数据内容的行数.这是一个菜单系统,假设在特定菜单中有5个选项,我只想在屏幕上显示5行,背景图像代替空行的位置(类似的例子,我想是世界时钟).我该怎么办呢?我已经四处搜寻但找不到任何有用的东西.非常感谢.

---------------------
Menu Item 1
---------------------
Menu Item 2
---------------------
Rest of view as image




---------------------
Run Code Online (Sandbox Code Playgroud)

使用分组表视图有效,但我真的不想使用它.

Pet*_*ese 4

你的UITableView可以有一个backgroundView。将其设置为 UIImageView,并将其框架设置为表视图的边界。该代码未经测试,但应该可以给您一个想法。

UIImage * img = [UIImage imageNamed:@"myImage.jpg"];
UIImageView * imgView = [[UIImageView alloc] initWithImage:img];
imgView.frame = myTableView.bounds;
myTableView.backgroundView = imgView;
Run Code Online (Sandbox Code Playgroud)

编辑:上面将产生一个像世界时钟的背景。要使其作为单元格显示在其他单元格下方,请更改您的数据源:

- 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;
Run Code Online (Sandbox Code Playgroud)

我不确定您是否必须设置图像视图大小,但您必须设置单元格大小以占用剩余空间,在表视图委托的 tableView:heightForRowAtIndexPath:最后一行中使用类似以下内容:

//if final row
return myTableView.bounds.size.height - 16.0 * myData.count; //modify from the default 16.0 as needed.
Run Code Online (Sandbox Code Playgroud)