有没有办法增加UITableViewCell之间的间距?

我已经创建了一个表,每个单元格只包含一个图像。图像被这样分配给单元格:

cell.imageView.image = [myImages objectAtIndex:indexPath.row];

但这使得图像放大并适合整个细胞,并且图像之间没有间隔。

或者让我们这样说,图像的高度是50,我想在图像之间增加20的间距。有什么办法可以做到吗?


当前回答

我认为最直接的解决方案,如果你只是寻找一点空间,可能最便宜的将是简单地设置单元格边框颜色为您的表格背景颜色,然后设置边框宽度,以获得所需的结果!

    cell.layer.borderColor = blueColor.CGColor
    cell.layer.borderWidth = 3

其他回答

我的情况是我使用自定义UIView viewForHeader在节也hightforheader在节返回常量高度说40,问题是当没有数据时,所有的头视图被彼此触摸。所以我想在没有数据的部分之间留出空间,所以我通过将“tableview style”平面改为“Group”来固定。这对我很有效。

如果您还没有使用节页眉(或页脚),您可以使用它们为表单元格添加任意间距。与其创建一个有n行的section,不如创建一个有n个section,每个section都有一行的表。

实现tableView: hightforheaderinsection:方法来控制间距。

你可能还想实现tableView:viewForHeaderInSection:来控制空格的样子。

使用节而不是行 每个section应该返回一行 使用indexPath分配单元格数据。Section,而不是row 实现UITableView委托方法hightforheader并返回你想要的间距

试着调查一下 ——(UIEdgeInsets) layoutMargins; 在单元格上

我需要做相同的概念,让UITableCells之间有一个“空间”。因为你不能在单元格之间添加空间,你可以通过操纵UITableView的单元格高度来伪造它,然后添加一个UIView到你的单元格的contentView。这是我在另一个测试项目中模拟的一个原型的屏幕截图:

下面是一些代码(注意:为了演示目的,有很多硬编码的值)

首先,我需要设置highightforrowatindexpath以允许UITableViewCell上的不同高度。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSString *text = [self.newsArray  objectAtIndex:[indexPath row]];
    if ([text isEqual:@"December 2012"])
    {
        return 25.0;
    }

    return 80.0;
}

接下来,我想操纵UITableViewCells的外观和感觉,所以我在willDisplayCell:(NewsUITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath方法中做到这一点。

- (void)tableView:(UITableView *)tableView willDisplayCell:(NewsUITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (cell.IsMonth)
    {
        UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 20, 20)];
        av.backgroundColor = [UIColor clearColor];
        av.opaque = NO;
        av.image = [UIImage imageNamed:@"month-bar-bkgd.png"];
        UILabel *monthTextLabel = [[UILabel alloc] init];
        CGFloat font = 11.0f;
        monthTextLabel.font = [BVFont HelveticaNeue:&font];

        cell.backgroundView = av;
        cell.textLabel.font = [BVFont HelveticaNeue:&font];
        cell.textLabel.textColor = [BVFont WebGrey];
    }


    if (indexPath.row != 0)
    {
        cell.contentView.backgroundColor = [UIColor clearColor];
        UIView *whiteRoundedCornerView = [[UIView alloc] initWithFrame:CGRectMake(10,10,300,70)];
        whiteRoundedCornerView.backgroundColor = [UIColor whiteColor];
        whiteRoundedCornerView.layer.masksToBounds = NO;
        whiteRoundedCornerView.layer.cornerRadius = 3.0;
        whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(-1, 1);
        whiteRoundedCornerView.layer.shadowOpacity = 0.5;
        [cell.contentView addSubview:whiteRoundedCornerView];
        [cell.contentView sendSubviewToBack:whiteRoundedCornerView];

    }
}

注意,我使我的whiteRoundedCornerView高度70.0,这就是模拟空间的原因,因为单元格的高度实际上是80.0,但我的contentView是70.0,这给了它外观。

也许还有其他更好的方法,但这就是我找到的方法。我希望它能帮助到其他人。