有没有办法增加UITableViewCell之间的间距?
我已经创建了一个表,每个单元格只包含一个图像。图像被这样分配给单元格:
cell.imageView.image = [myImages objectAtIndex:indexPath.row];
但这使得图像放大并适合整个细胞,并且图像之间没有间隔。
或者让我们这样说,图像的高度是50,我想在图像之间增加20的间距。有什么办法可以做到吗?
有没有办法增加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.
明白吗?
其他回答
试着调查一下 ——(UIEdgeInsets) layoutMargins; 在单元格上
如果你不想改变你的表视图的section和行号(就像我做的那样),下面是你要做的:
1)在表格单元格视图的底部添加一个ImageView。
2)使它与表格视图的背景颜色相同。
我已经在我的应用程序中这样做了,它工作得很好。干杯!: D
TableViewCell - - - >
override open var frame: CGRect {
get {
return super.frame
}
set {
var frame = newValue
frame.size.height -= 2
super.frame = frame
}
}
我认为这是最干净的解决方案:
class MyTableViewCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
layoutMargins = UIEdgeInsetsMake(8, 0, 8, 0)
}
}
查看我在GitHub上的解决方案,其中包含UITableView的子类,并使用Objective-C的运行时特性。 它基本上使用了苹果的私有数据结构uitableviewwrodata,我得到了搜索UITableView的私有运行时头:
https://github.com/JaviSoto/iOS10-Runtime-Headers/blob/master/Frameworks/UIKit.framework/UITableView.h,
这里是你想要的私有类,它包含了你需要的所有东西来布局你的单元格的空间,而不需要在单元格的类中设置它:
https://github.com/JaviSoto/iOS10-Runtime-Headers/blob/master/Frameworks/UIKit.framework/UITableViewRowData.h