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

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

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

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

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


当前回答

为了增加行之间的间距,将使用第二个原型单元格。

在第二个原型单元格中添加一个不同的单元格标识符,然后添加一个空视图,并将其边缘约束到单元格的边缘,然后将emptyView的颜色设置为“clear”。

然后在代码中:

import UIKit

class YourViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
        
        @IBOutlet weak var yourTableView: UITableView!
        
        let yourArray = ["data1", "data2", "data3" /* so on */ ]

        let emptyTransparentRowHeight: CGFloat = 10
        
        override func viewDidLoad() {
            super.viewDidLoad()
    
            yourTableView.dataSource = self
            yourTableView.delegate = self
        }
            

        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return (yourArray.count * 2)
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
            guard indexPath.row % 2 == 0,
                let usableCell = tableView.dequeueReusableCell(withIdentifier: "Your First Cell Prototype Identifier") as? YourTableViewCellSubClass
            else {
                
                return tableView.dequeueReusableCell(withIdentifier: "The Second and Empty Cell Prototype Identifier")!
            }
      
            usableCell.Label?.text = yourArray[indexPath.row/2]
            
            return usableCell
    
        }
        
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            if indexPath.row % 2 == 0 {
                return tableView.rowHeight
            }
            
            return self.emptyTransparentRowHeight
        }
        
    }

其他回答

只是把对我有用的答案添加到答案池中。

我已经在TableViewCell中添加了一个视图(紫色视图),我使用它作为单元格的内容视图。并限制紫色视图在顶部和底部都有填充,或者其他你想要的,但我认为这种方式创造了更多的灵活性。

在阅读其他人的答案后再读这篇文章

我想警告所有想要使用像添加标题这样的解决方案来达到间隔目的的人。如果你这样做,你将不能动画单元格的插入,删除等。例如,如果您使用该方法,您可能会得到这种错误

无效更新:无效的节数。更新后(6)表视图中包含的section数量必须等于更新前(5)表视图中包含的section数量,加上或减去插入或删除的section数量(插入0个,删除0个)。

如果你需要动画插入和删除行,我会在单元格本身添加这个空间。如果你担心高亮显示,那么你可以重写方法 func setHighlighted(_ highlighted: Bool, animated: Bool) 然后自己设置高亮

我需要做相同的概念,让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,这给了它外观。

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

根据Husam的回答:使用单元格层而不是内容视图允许在整个单元格和附件周围添加边框。这种方法需要仔细调整单元格的底部约束以及那些嵌入,否则视图将不正确。

@implementation TableViewCell

- (void)awakeFromNib { 
    ... 
}

- (void) layoutSubviews {
    [super layoutSubviews];

    CGRect newFrame = UIEdgeInsetsInsetRect(self.layer.frame, UIEdgeInsetsMake(4, 0, 4, 0));
    self.layer.frame = newFrame;
}

@end

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