分组的UITableView中第一个和第二个单元格之间的1像素间隙

这似乎是一个简单的问题,但我碰到了一堵砖墙寻找解决scheme,我希望有人在这里可以帮助…

我正在UITableView使用UITableViewStyleGrouped,我看到我的表视图中的每个部分的第一行和第二行之间的一个小(1像素)的白线(见图片)

差距的例子

看来这条线的原因是每个部分的第一个单元比其他单元高1个像素。 当我把每个组的初始单元设置为29像素高(其余为30)时,间隙消失,一切正常。

虽然这是解决方法是成功的,我宁愿知道发生这种情况的原因,所以我可以避免使用一个凌乱的黑客。

供参考:

  • 我已经确定这不是我使用的背景图像的问题 – 他们都是30px高,我已经用中间图像replace顶部图像,结果相同。
  • 我已经通过设置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中设置支持Image的代码:

 - (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)]; 

这是一个已知的问题,只有当你隐藏分组tableView的分隔符时,发生在这个其他堆栈溢出问题完全相同的事情。

简而言之,顶部和底部单元格使用一个额外的 ,但分隔符隐藏它,所以解决scheme是:

  1. 减去额外的间距(就像你已经做的那样)
  2. 使背景成为重复的图案或可拉伸的图像(不是为了宣传自己,而是我在上一个链接线索上给出的答案也解释了如何做可拉伸的图像)。
  3. 不要隐藏分隔符,但匹配是与单元格的背景颜色,所以它看起来像它的隐藏。

选项2似乎是“最干净的”,并增加了支持tableView单元格的高度的好处。

为什么有这个额外的 ? 只有苹果知道。

根据我的经验,分组表视图在每个部分的第一行和最后一行的高度添加1点(而不是像素!)。 您应该能够通过添加debugging代码来覆盖您的表格单元类中的layoutSubviews,将其调用超级,然后打印bounds.size.height。

我认为这是为边界绘图提供空间,和你一样,我刚刚调整了我为第一个和最后一个单元格的高度返回的值来抵消效果。 由于视觉外观已经改变,这在iOS 7中可能会改变。

[编辑]这是检查视图层次结构的debugging代码的好位。 (你不能直接调用recursiveDescription ,因为它是私有的,你的代码不能在默认的Xcode设置下编译。)

NSLog(@"%@", [self.view performSelector:@selector(recursiveDescription)]);

如果你这样做,它会解决你的问题:摆脱'backingImage'(backingView)。 代替使用单元格的backgroundView属性,只需将UIImageView分配给该单元格即可。 现在当你为背景创build你的UIImageView时,请确保你的UIImage是这样的:

 UIImage *bg = [UIImage imageName:@"filename.png"]; bg = [bg resizableImageWithCapInsets:UIEdgeInsetsMake(15, 0, 15, 0)]; 

注意:我假设你的图像有30点的高度。 看看你使用的图像可以被垂直拉伸,我在这个例子中做了什么。 为了节省更多的内存,你也可以水平拉伸它,但是你拥有的资产可能不适合这个。

希望这可以帮助!

这是尖叫部分标题给我。 你重写viewForHeaderInSection方法吗? 我最好的猜测是,每个部分中的第一个单元格的边界以某种方式消除了该部分的头部边界。

尝试覆盖这些方法:

 – tableView:viewForHeaderInSection: – tableView:viewForFooterInSection: – tableView:heightForHeaderInSection: – tableView:heightForFooterInSection: – tableView:willDisplayHeaderView:forSection: – tableView:willDisplayFooterView:forSection: 

并玩他们回报。 特别是heightForHeaderInSection / viewForHeaderInSection / willDisplayHeaderView:ForSection:

在没有分组tableview的分隔符的iOS 8上,我也遇到了这个问题。 我的部分之间有0.5f的差距。 由于我的tableview后面的背景是不一样的颜色,这是非常明显的。 解决的办法是用一个空白的headerview填充1px高,然后用另一个视图溢出1px。 奇迹般有效。

 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 1)]; view.backgroundColor = [UIColor whiteColor]; view.opaque = YES; view.clipsToBounds = NO; //Spill a view out by one pixel. UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 2)]; subView.backgroundColor = [UIColor whiteColor]; subView.opaque = YES; [view addSubview:subView]; return view; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 1; }