【发布时间】:2011-05-19 18:38:39
【问题描述】:
我似乎不知道如何隐藏没有数据的表格单元格。在分组视图中,每条数据只能获得一个单元格。而在普通视图中,它将单元格渲染到屏幕底部。我确信它是可能的,因为世界时钟可以做到这一点
【问题讨论】:
标签: iphone ios uitableview
我似乎不知道如何隐藏没有数据的表格单元格。在分组视图中,每条数据只能获得一个单元格。而在普通视图中,它将单元格渲染到屏幕底部。我确信它是可能的,因为世界时钟可以做到这一点
【问题讨论】:
标签: iphone ios uitableview
Andy 的This answer 是解决此问题的最简单方法。它告诉表格视图有一个页脚(大小为零),并防止绘制分隔线(呈现空行的外观)。
【讨论】:
为了获得这个世界时钟效果,我为 UITableView 背景设置了一个背景图片,并将表格背景颜色设置为 clearColor,方法是:
UIImage *image = [UIImage imageNamed:@"background.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView setFrame:self.tableView.bounds];
[self.tableView setBackgroundView:imageView];
[imageView release];
[self.tableView setBackgroundColor:[UIColor clearColor]];
为了摆脱分隔符,我将分隔符样式更改为 none via
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
我为 UITableViewCell 设置了背景颜色(这里:白色):
UIView *cellBackgroundView = [[UIView alloc] init];
[cellBackgroundView setBackgroundColor:[UIColor whiteColor]];
[cell setBackgroundView:cellBackgroundView];
[cellBackgroundView release];
【讨论】:
无论表的类型如何,它都会填满 Interface Builder 中分配给它的空间(或者任何分配的框架,等等,如果您以编程方式创建它)。
您可能想要做的是将背景设置为透明(使用“清除颜色”选项作为界面生成器中的背景颜色)并使用自定义单元格背景,我相信这是世界时钟所做的。
为此,只需在 tableView:cellForRowAtIndexPath: 方法中返回的表格单元格中实现背景,方法是创建一个视图并在 UITableViewCell 上适当地使用 setBackgroundView 和 setSelectedBackgroundView 方法。
【讨论】:
backgroundColor = [UIColor colorWithWhite:1 alpha:0]; backgroundView = nil; 和使用上述说明的单元格背景。