有没有办法增加UITableViewCell之间的间距?
我已经创建了一个表,每个单元格只包含一个图像。图像被这样分配给单元格:
cell.imageView.image = [myImages objectAtIndex:indexPath.row];
但这使得图像放大并适合整个细胞,并且图像之间没有间隔。
或者让我们这样说,图像的高度是50,我想在图像之间增加20的间距。有什么办法可以做到吗?
有没有办法增加UITableViewCell之间的间距?
我已经创建了一个表,每个单元格只包含一个图像。图像被这样分配给单元格:
cell.imageView.image = [myImages objectAtIndex:indexPath.row];
但这使得图像放大并适合整个细胞,并且图像之间没有间隔。
或者让我们这样说,图像的高度是50,我想在图像之间增加20的间距。有什么办法可以做到吗?
当前回答
这篇文章很有帮助,它和其他答案差不多,但是总结和简洁
https://medium.com/@andersongusmao/left-and-right-margins-on-uitableviewcell-595f0ba5f5e6
其中,他只将它们应用于左侧和右侧,但UIEdgeInsetsMake初始化允许为所有四个点添加填充。
func UIEdgeInsetsMake(_ top: CGFloat, _ left: CGFloat, _ bottom: CGFloat, _ right: CGFloat) -> UIEdgeInsets Description Creates an edge inset for a button or view. An inset is a margin around a rectangle. Positive values represent margins closer to the center of the rectangle, while negative values represent margins further from the center. Parameters top: The inset at the top of an object. left: The inset on the left of an object bottom: The inset on the bottom of an object. right: The inset on the right of an object. Returns An inset for a button or view
注意,UIEdgeInsets也可以用来实现同样的效果。
Xcode 9.3/Swift 4
其他回答
查看我在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
是的,你可以增加或减少间距(填充)之间的两个单元格通过创建一个基本视图的内容视图在单元格。为内容视图背景设置清晰的颜色,您可以调整基本视图的高度以创建单元格之间的空间。
我认为这是最干净的解决方案:
class MyTableViewCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
layoutMargins = UIEdgeInsetsMake(8, 0, 8, 0)
}
}
使用节而不是行 每个section应该返回一行 使用indexPath分配单元格数据。Section,而不是row 实现UITableView委托方法hightforheader并返回你想要的间距
TableViewCell - - - >
override open var frame: CGRect {
get {
return super.frame
}
set {
var frame = newValue
frame.size.height -= 2
super.frame = frame
}
}