有没有办法增加UITableViewCell之间的间距?
我已经创建了一个表,每个单元格只包含一个图像。图像被这样分配给单元格:
cell.imageView.image = [myImages objectAtIndex:indexPath.row];
但这使得图像放大并适合整个细胞,并且图像之间没有间隔。
或者让我们这样说,图像的高度是50,我想在图像之间增加20的间距。有什么办法可以做到吗?
有没有办法增加UITableViewCell之间的间距?
我已经创建了一个表,每个单元格只包含一个图像。图像被这样分配给单元格:
cell.imageView.image = [myImages objectAtIndex:indexPath.row];
但这使得图像放大并适合整个细胞,并且图像之间没有间隔。
或者让我们这样说,图像的高度是50,我想在图像之间增加20的间距。有什么办法可以做到吗?
当前回答
将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
}
其他回答
查看我在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
根据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
你必须为你的图像设置框架。未测试的代码是
cell.imageView.frame = CGRectOffset(cell.frame, 10, 10);
我认为最直接的解决方案,如果你只是寻找一点空间,可能最便宜的将是简单地设置单元格边框颜色为您的表格背景颜色,然后设置边框宽度,以获得所需的结果!
cell.layer.borderColor = blueColor.CGColor
cell.layer.borderWidth = 3
我可以想到三种方法:
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.
明白吗?