这似乎是一个简单的问题,但我找到了解决方案的砖墙,我希望这里有人可以提供帮助......
我正在使用UITableViewStyleGrouped创建一个UITableView,我看到我的表视图的每个部分的第一行和第二行之间有一条小的(1像素)白线(见图)

似乎这一行的原因是每个部分中的第一个单元格比其他单元格高1个像素.当我将每组的初始单元设置为29像素高(其他组为30)时,间隙消失并且一切都很好.
虽然这是成功的解决方法,但我宁愿知道发生这种情况的原因,所以我可以避免使用凌乱的黑客攻击.
供参考:
任何帮助是极大的赞赏.
谢谢
表格设置代码:
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;
Run Code Online (Sandbox Code Playgroud)
细胞设置代码:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
在MyTableViewCell中设置支持Image的代码:
- (void)setBackingImage:(UIImage *)backingImage
{
if (self.backingImage != backingImage)
{
_backingImage = nil;
_backingImage = backingImage;
self.backingView.image = backingImage;
}
}
Run Code Online (Sandbox Code Playgroud)
使用以下代码设置self.backingView:
[[UIImageView alloc] initWithFrame:CGRectMake(7, 0, 307, 30)];
Run Code Online (Sandbox Code Playgroud)
这是一个已知问题,仅当您隐藏分组 tableView 的分隔符时才会发生,在另一个堆栈溢出问题中也发生了完全相同的事情。
简单来说,顶部和底部的单元格使用了一个额外的点,但是分隔符将其隐藏了,所以解决方案是:
选项 2 似乎是“最干净的”,并且增加了支持 tableView 单元格可变高度的好处。
为什么它有这个额外点?只有苹果知道。
| 归档时间: |
|
| 查看次数: |
3881 次 |
| 最近记录: |