有没有办法增加UITableViewCell之间的间距?
我已经创建了一个表,每个单元格只包含一个图像。图像被这样分配给单元格:
cell.imageView.image = [myImages objectAtIndex:indexPath.row];
但这使得图像放大并适合整个细胞,并且图像之间没有间隔。
或者让我们这样说,图像的高度是50,我想在图像之间增加20的间距。有什么办法可以做到吗?
有没有办法增加UITableViewCell之间的间距?
我已经创建了一个表,每个单元格只包含一个图像。图像被这样分配给单元格:
cell.imageView.image = [myImages objectAtIndex:indexPath.row];
但这使得图像放大并适合整个细胞,并且图像之间没有间隔。
或者让我们这样说,图像的高度是50,我想在图像之间增加20的间距。有什么办法可以做到吗?
当前回答
使用节而不是行 每个section应该返回一行 使用indexPath分配单元格数据。Section,而不是row 实现UITableView委托方法hightforheader并返回你想要的间距
其他回答
你必须为你的图像设置框架。未测试的代码是
cell.imageView.frame = CGRectOffset(cell.frame, 10, 10);
使用UITableViewDelegate, highightforrowatindexpath并返回行高。
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100.0f ;
}
你可以像这样简单地在代码中使用constraint:
class viewCell : UITableViewCell
{
@IBOutlet weak var container: UIView!
func setShape() {
self.container.backgroundColor = .blue
self.container.layer.cornerRadius = 20
container.translatesAutoresizingMaskIntoConstraints = false
self.container.widthAnchor.constraint(equalTo:contentView.widthAnchor , constant: -40).isActive = true
self.container.heightAnchor.constraint(equalTo: contentView.heightAnchor,constant: -20).isActive = true
self.container.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
self.container.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
}
}
添加子视图(容器)并在其中放入其他元素是很重要的。
如果您还没有使用节页眉(或页脚),您可以使用它们为表单元格添加任意间距。与其创建一个有n行的section,不如创建一个有n个section,每个section都有一行的表。
实现tableView: hightforheaderinsection:方法来控制间距。
你可能还想实现tableView:viewForHeaderInSection:来控制空格的样子。
我在Swift 4中是这样解决的。
我创建了UITableViewCell的扩展,并包括以下代码:
override open var frame: CGRect {
get {
return super.frame
}
set (newFrame) {
var frame = newFrame
frame.origin.y += 10
frame.origin.x += 10
frame.size.height -= 15
frame.size.width -= 2 * 10
super.frame = frame
}
}
override open func awakeFromNib() {
super.awakeFromNib()
layer.cornerRadius = 15
layer.masksToBounds = false
}
我希望这对你有所帮助。