【问题标题】:Spacing between cells on UITableView with Custom UITableViewCell使用自定义 UITableViewCell 在 UITableView 上的单元格之间的间距
【发布时间】:2012-06-01 11:15:02
【问题描述】:

我有一个 UITableView 从 XIB 文件加载自定义 UITableViewCell。一切正常,但我的布局要求单元格(都在一个部分内)之间有间距。 有没有机会在不必提高 ROW 高度的情况下做到这一点?

现在怎么样了

应该如何

编辑:
这就是今天的代码

- (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;
}

【问题讨论】:

  • 为什么不想提高行高?
  • @obuseme 在最简单的情况下有一个xib和三个行状态(中、上、下),代码会很纠结。
  • @obuseme 另外,我的单元格有一个背景图像,标签所在的高度为 35。如果我提高行高,布局将是我想要的,但点击选择将在图像下方的间距上起作用,并且我不希望意外选择错误的行。
  • Answer for Swift(每个数组元素方法一节)

标签: iphone objective-c cocoa-touch uitableview


【解决方案1】:

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

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

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

这将在您放入调用此单元格的 uitablview 类中的单元格高度内为您提供 4pt 缓冲区。显然,您现在必须在放入标签和图像时补​​偿单元格中较少的空间,但它作品。您也可以在 x 轴上执行相同的操作,以使单元格的宽度更小。我已经在我的应用中这样做了,以在我的单元格后面显示背景图像。

【讨论】:

  • 谢谢你这就是我要找的东西:)
  • 它正在工作;但自动布局损坏;我正在研究可扩展单元;当按下单元格以展开展开的单元格时,不调整所有文本;你能建议吗
【解决方案2】:

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

- (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;
}

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

【讨论】:

  • 是否有机会提供有关如何完成此操作的代码示例?我将我当前的代码添加到问题中。谢谢。
  • 实际上,这个唯一的代码使所有偶数行都为空。例如,第一行(单元格 1)返回 0,因此不是返回带有文本的单元格,而是返回单元格 2 空白。所以我在表中得到 2 个项目,但第一个是空白的。
  • @Guilherme 对,用indexPath.row % 2 == 1 更正它,偶数行不可见,我的意思是 2,4,6,... 从 1 开始计数。
  • 成功了!我添加到代码中的唯一内容是[cell2 setUserInteractionEnabled:NO],因此这些单元格禁用了触摸。非常感谢您的帮助。
  • 这是最简单的方法,到目前为止我发现了,但是如果你想避免额外的过载,请尝试在 UIView 上添加元素,而不是直接在内容视图中。另外,相应地添加约束。
【解决方案3】:

虽然@A-Live 在技术上是正确的,但为了防止出现其他问题,例如:您忘记将 indexPath.row/2 放在某处,您可以执行以下操作(不涉及编程):

例如,您的 UITableViewCell 高度通常为 90 磅,而您希望每个单元格之间有 10 磅的间距。使您的 UITableViewCell 高 100 点,并将 UITableViewCell 的底部 10 点设置为空白(没有任何类型的对象)。然后单击界面生成器中的 UITableViewCell 并将 backgroundColor 设置为您想要的间距区域颜色。

瞧!您只需做一点工作就可以在每个单元格之间留出间距,这比在任何地方都编程 /2 容易得多!

【讨论】:

  • 我如何在没有任何东西的情况下进入后 10 名?我只是将单元格的背景设置为白色
【解决方案4】:

如果您正在寻找快速解决方案,reddit 的这个答案对我来说非常适合。

class CustomTableViewCell: UITableViewCell {
override var frame: CGRect {
    get {
        return super.frame
    }
    set (newFrame) {
        var frame =  newFrame
        frame.origin.y += 4
        frame.size.height -= 2 * 4
        super.frame = frame
    }
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多