有没有办法增加UITableViewCell之间的间距?

我已经创建了一个表,每个单元格只包含一个图像。图像被这样分配给单元格:

cell.imageView.image = [myImages objectAtIndex:indexPath.row];

但这使得图像放大并适合整个细胞,并且图像之间没有间隔。

或者让我们这样说,图像的高度是50,我想在图像之间增加20的间距。有什么办法可以做到吗?


当前回答

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

在第二个原型单元格中添加一个不同的单元格标识符,然后添加一个空视图,并将其边缘约束到单元格的边缘,然后将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
        }
        
    }

其他回答

如果您还没有使用节页眉(或页脚),您可以使用它们为表单元格添加任意间距。与其创建一个有n行的section,不如创建一个有n个section,每个section都有一行的表。

实现tableView: hightforheaderinsection:方法来控制间距。

你可能还想实现tableView:viewForHeaderInSection:来控制空格的样子。

是的,你可以增加或减少间距(填充)之间的两个单元格通过创建一个基本视图的内容视图在单元格。为内容视图背景设置清晰的颜色,您可以调整基本视图的高度以创建单元格之间的空间。

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

在第二个原型单元格中添加一个不同的单元格标识符,然后添加一个空视图,并将其边缘约束到单元格的边缘,然后将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
        }
        
    }

TableViewCell - - - >

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

我重写这个函数是UITableViewCell的子类,它对我来说工作正常

override func layoutSubviews() {
      super.layoutSubviews()
      //set the values for top,left,bottom,right margins
      let margins = UIEdgeInsets(top: 5, left: 8, bottom: 5, right: 8)
      contentView.frame = contentView.frame.inset(by: margins)
      contentView.layer.cornerRadius = 8
}