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

hea*_*des 16 uitableview ios

这似乎是一个简单的问题,但我找到了解决方案的砖墙,我希望这里有人可以提供帮助......

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

差距的例子

似乎这一行的原因是每个部分中的第一个单元格比其他单元格高1个像素.当我将每组的初始单元设置为29像素高(其他组为30)时,间隙消失并且一切都很好.

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

供参考:

  • 我已经确定这不是我使用的背景图像的问题 - 它们都是30px高,并且我已经用具有相同结果的中间图像替换了顶部图像.
  • 我通过设置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;
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)

Can*_*Can 3

这是一个已知问题,仅当您隐藏分组 tableView 的分隔符时才会发生,在另一个堆栈溢出问题中也发生了完全相同的事情。

简单来说,顶部和底部的单元格使用了一个额外的,但是分隔符将其隐藏了,所以解决方案是:

  1. 减去额外的间距(就像你已经做的那样)
  2. 将背景设为重复图案或可拉伸图像(不是想推销自己,但我在上一个链接线程中给出的答案也解释了如何制作可拉伸图像)。
  3. 不要隐藏分隔符,但颜色与单元格的背景相匹配,因此看起来像是隐藏的。

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

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