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

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

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

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

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


当前回答

我可以想到三种方法:

Create a custom table cell that lays out the view of the entire cell in the manner that you desire Instead of adding the image to the image view, clear the subviews of the image view, create a custom view that adds an UIImageView for the image and another view, perhaps a simple UIView that provides the desired spacing, and add it as a subview of the image view. I want to suggest that you manipulate the UIImageView directly to set a fixed size/padding, but I'm nowhere near Xcode so I can't confirm whether/how this would work.

明白吗?

其他回答

如果你不想改变你的表视图的section和行号(就像我做的那样),下面是你要做的:

1)在表格单元格视图的底部添加一个ImageView。

2)使它与表格视图的背景颜色相同。

我已经在我的应用程序中这样做了,它工作得很好。干杯!: D

如果你不想使用任何标题,使用标题作为间距也可以。否则,这可能不是最好的主意。我在想的是创建一个自定义单元格视图。

例子:

用笔尖 在代码中

在自定义单元格中,创建一个带有约束的背景视图,这样它就不会填充整个单元格,给它一些填充。

然后,使tableview背景不可见,并删除分隔符:

// Make the background invisible
tableView.backgroundView = UIView()
tableView.backgroundColor = .clear

// Remove the separators
tableview.separatorStyle = .none

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

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

是的,你可以增加或减少间距(填充)之间的两个单元格通过创建一个基本视图的内容视图在单元格。为内容视图背景设置清晰的颜色,您可以调整基本视图的高度以创建单元格之间的空间。

将section中的行数更改为1 您更改了区段数而不是行数

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        1
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }

这里是行与行之间的行距

 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }