有没有办法增加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

其他回答

我在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
}

我希望这对你有所帮助。

TableViewCell - - - >

   override open var frame: CGRect {
    get {
        return super.frame
    }
    set {
        var frame =  newValue
        frame.size.height -= 2
        super.frame = frame
    }
}

试着调查一下 ——(UIEdgeInsets) layoutMargins; 在单元格上

为了增加行之间的间距,将使用第二个原型单元格。

在第二个原型单元格中添加一个不同的单元格标识符,然后添加一个空视图,并将其边缘约束到单元格的边缘,然后将emptyView的颜色设置为“clear”。

然后在代码中:

import UIKit

class YourViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
        
        @IBOutlet weak var yourTableView: UITableView!
        
        let yourArray = ["data1", "data2", "data3" /* so on */ ]

        let emptyTransparentRowHeight: CGFloat = 10
        
        override func viewDidLoad() {
            super.viewDidLoad()
    
            yourTableView.dataSource = self
            yourTableView.delegate = self
        }
            

        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return (yourArray.count * 2)
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
            guard indexPath.row % 2 == 0,
                let usableCell = tableView.dequeueReusableCell(withIdentifier: "Your First Cell Prototype Identifier") as? YourTableViewCellSubClass
            else {
                
                return tableView.dequeueReusableCell(withIdentifier: "The Second and Empty Cell Prototype Identifier")!
            }
      
            usableCell.Label?.text = yourArray[indexPath.row/2]
            
            return usableCell
    
        }
        
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            if indexPath.row % 2 == 0 {
                return tableView.rowHeight
            }
            
            return self.emptyTransparentRowHeight
        }
        
    }

我认为最直接的解决方案,如果你只是寻找一点空间,可能最便宜的将是简单地设置单元格边框颜色为您的表格背景颜色,然后设置边框宽度,以获得所需的结果!

    cell.layer.borderColor = blueColor.CGColor
    cell.layer.borderWidth = 3