【发布时间】:2013-01-31 23:28:27
【问题描述】:
这似乎是一个简单的问题,但我在寻找解决方案时遇到了困难,我希望这里有人可以提供帮助...
我正在使用 UITableViewStyleGrouped 制作 UITableView,我在表格视图的每个部分的第一行和第二行之间看到一条小的(1 像素)白线(见图)
这条线的原因似乎是每个部分的第一个单元格比其他单元格高 1 个像素。当我将每组的初始单元格设置为 29 像素高(其他为 30 像素)时,间隙消失了,一切都很好。
虽然这是一种成功的解决方法,但我宁愿知道发生这种情况的原因,这样我就可以避免使用凌乱的 hack。
仅供参考:
- 我已确保这不是我使用的背景图片的问题 - 它们都是 30 像素高,我已将顶部图片替换为具有相同结果的中间图片。
- 我通过设置 separatorStyle = UITableViewCellSeparatorStyleNone 删除了单元格之间的所有边框
- UITableViewStylePlain 上不会出现这种效果
非常感谢任何帮助。
谢谢
表格设置代码:
myTable = [[UITableView alloc] initWithFrame:CGRectMake(TABLE_SHIFT_OFFSET,
DATE_SLIDER_HEIGHT - DATE_SLIDER_SHADOW_HEIGHT,
326,
self.view.frame.size.height - DATE_SLIDER_HEIGHT + DATE_SLIDER_SHADOW_HEIGHT) style:UITableViewStyleGrouped];
myTable.backgroundColor = [UIColor clearColor];
myTable.backgroundView = nil;
myTable.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:myTable];
myTable.dataSource = self;
myTable.delegate = self;
单元设置代码:
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// This is a hack to make sure that a white line doesnt appear between the
// first and second rows in the table, which happens for reasons I dont understand
if (indexPath.row == 0) {
return 29;
}
return 30;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"satCell"];
if (!cell)
{
cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"satCell"];
}
cell.backgroundView = nil;
cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
cell.backgroundColor = [UIColor whiteColor];
int numberOfRowsInThisSection = [self tableView:tableView numberOfRowsInSection:indexPath.section];
if (numberOfRowsInThisSection == 1)
{
cell.backingImage = self.singleWhite;
}
else if (indexPath.row == 0)
{
cell.backingImage = self.topWhite;
}
else if (indexPath.row % 2 == 0)
{
// Even row
if (indexPath.row == numberOfRowsInThisSection - 1)
{
// Last row (even)
cell.backingImage = self.botWhite;
}
else
{
// Normal row (even)
cell.backingImage = self.midWhite;
}
}
else if (indexPath.row % 2 == 1)
{
// Odd row
if (indexPath.row == numberOfRowsInThisSection - 1)
{
// Last row (even)
cell.backingImage = self.botDark;
}
else
{
// Normal row (odd)
cell.backingImage = self.midDark;
}
}
// Setup cell text [REMOVED FOR BREVITY]
return cell;
}
MyTableViewCell 中设置背景图片的代码:
- (void)setBackingImage:(UIImage *)backingImage
{
if (self.backingImage != backingImage)
{
_backingImage = nil;
_backingImage = backingImage;
self.backingView.image = backingImage;
}
}
self.backingView 使用以下代码设置:
[[UIImageView alloc] initWithFrame:CGRectMake(7, 0, 307, 30)];
【问题讨论】:
-
我们能看到一些代码吗?你在 heightForRowAtIndexPath: 中有什么事情吗?
-
单元格视图相关的代码会很好。您要向单元格中的哪个视图添加视图,单元格本身,contentView 还是 backgroundView?您添加自己的带有边框的图像?我做了一些测试,我认为分隔符样式不适用于分组表,它们总是绘制分隔符。
-
我已经添加了所有我能想到的可能会影响表结构的代码。如果需要,我很乐意添加任何其他内容... PS。 heightForRowAtIndexPath: 中的破解是我目前的解决方法。如果黑客被删除,差距会重新出现
-
只有添加背景图片才会出现while行吗?如果您没有单元格的任何背景图像怎么办?既然图像中没有不规则的图案,为什么不将单元格的颜色设置为 [UIColor colorWithPattern:backingImage]; ? (我也很想知道为什么会出现白线)
-
在这种情况下,边框包含在图像中,因此带图案的图像会产生不需要的效果。降低单元格高度的技巧似乎是一个合理的解决方法,但在分组表视图中,第一个单元格高 1 个像素似乎很奇怪(我想知道为什么)。这与使用的图像无关 - 图像只是突出显示效果
标签: ios uitableview