UITableView上的单元格与自定义UITableViewCell之间的间距

gmo*_*mes 8 iphone cocoa-touch objective-c uitableview

我有一个UITableView从XIB文件加载自定义UITableViewCell.一切都工作正常,但我的布局要求单元格(都在一个单独的部分内)之间有间距.有没有机会这样做而不必提高行高?

现在怎么样
现在怎么样

这是怎么回事
这是怎么回事

编辑:
这就是今天的代码

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[self.cards valueForKeyPath:@"cards"] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    [ccTableView setBackgroundColor:[UIColor clearColor]];
    cardsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cardsCell"];

    if(cell == nil){
      NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"cardsCell" owner:self options:nil];
      cell = [topLevelObjects objectAtIndex:0];
    }

    NSString *nmCard = [[self.cards valueForKeyPath:@"cards.name"] objectAtIndex:indexPath.row];
    cell.descCardLabel.text = nmCard;

  return cell;
}
Run Code Online (Sandbox Code Playgroud)

小智 37

如果您使用自定义的uitableviewcell类,我发现它实际上是一个非常简单的解决方案.

在单元类中,添加以下代码:

- (void)setFrame:(CGRect)frame {
    frame.origin.y += 4;
    frame.size.height -= 2 * 4;
    [super setFrame:frame];
}
Run Code Online (Sandbox Code Playgroud)

这将为你在你调用这个单元格的uitablview类中的单元格高度提供一个4pt的缓冲区.显然,你现在必须在放入标签和图像时补​​偿单元格中较少的空间,但是它有效.您也可以在x轴上执行相同的操作,以使单元格的宽度更小.我在我的应用程序中完成了这项操作,以显示我的单元格后面的背景图像


A-L*_*ive 7

如果无法更改单元格的高度,解决方案是使用所需高度的不可见中间单元格.在这种情况下,您需要在表视图委托和数据源中重新计算索引.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CELL_ID2 = @"SOME_STUPID_ID2";
    // even rows will be invisible
    if (indexPath.row % 2 == 1)
    {
        UITableViewCell * cell2 = [tableView dequeueReusableCellWithIdentifier:CELL_ID2];

        if (cell2 == nil)
        {
            cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                          reuseIdentifier:CELL_ID2];
            [cell2.contentView setAlpha:0];
            [cell2 setUserInteractionEnabled:NO]; // prevent selection and other stuff
        }
        return cell2;
    }

    [ccTableView setBackgroundColor:[UIColor clearColor]];
    cardsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cardsCell"];

    if(cell == nil){
      NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"cardsCell" owner:self options:nil];
      cell = [topLevelObjects objectAtIndex:0];
    }

     // Use indexPath.row/2 instead of indexPath.row for the visible section to get the correct datasource index (number of rows is increased to add the invisible rows)
        NSString *nmCard = [[self.cards valueForKeyPath:@"cards.name"] objectAtIndex:(indexPath.row/2)];
        cell.descCardLabel.text = nmCard;

      return cell;
    }



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    // two times minus one (invisible at even rows => visibleCount == invisibleCount+1)
    return [[self.cards valueForKeyPath:@"cards"] count] * 2 - 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row % 2 == 1)
        return 40;
    return 162;
}
Run Code Online (Sandbox Code Playgroud)

您还需要重新计算indexPath.row :didSelectRowAtIndexPath:以及使用它的其他方法.