简体   繁体   English

在Swift 3中的单元格之间添加空间

[英]Add space between cells in Swift 3

I want to add space between the cells that I have in my UITableView . 我想在UITableView的单元格之间添加空间。

Here's my code. 这是我的代码。 What I mean is cell then space then cell then space etc. 我的意思是细胞然后是空间,然后是细胞,然后是空间等等。

class TableViewController: UITableViewController {
    var textArray = [String]()
    var cellsArray = ["1","2","3"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        textArray = ["","",""]
        self.tableView.estimatedSectionHeaderHeight = 80
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return textArray.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: (cellsArray[indexPath.row]), for: indexPath) as UITableViewCell
        return cell
    }
}

Use this for space between two cells 将此用于两个单元格之间的空间

let testView: UIView = UIView(frame: CGRect(x:0, y:0, width:TableView1.frame.size.width , height:(cell?.contentView.frame.size.height)! - 5 ))
testView.backgroundColor = UIColor.blue
testView.alpha = 0.5
testView.isUserInteractionEnabled = true
testView.layer.cornerRadius = 5
testView.layer.masksToBounds = true
cell?.contentView.addSubview(testView)
cell?.contentView.sendSubview(toBack: testView)

Just simple approach if you like it ... 如果您喜欢,只是简单的方法...

let tableViewCellIdentifier = "TableViewCell"

class ViewController: UITableViewController {

    var cellsArray = ["1","2","3","4","5","6"]

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.separatorColor = UIColor.clear
        tableView.register(UINib(nibName: tableViewCellIdentifier, bundle: nil), forCellReuseIdentifier: tableViewCellIdentifier)
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cellsArray.count
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: tableViewCellIdentifier) as! TableViewCell

        cell.customCellLabel.text = cellsArray[indexPath.row]

        return cell
    }
}

Custom cell 自定义单元

class TableViewCell: UITableViewCell {
    @IBOutlet weak var customCellLabel: UILabel!
    @IBOutlet weak var innerView: UIView!
}

描述

在此处输入图片说明

If you have a vertical scroll for your UITableViewController or UICollectionViewController you need to add this code: 如果您的UITableViewControllerUICollectionViewController具有垂直滚动,则需要添加以下代码:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 10
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM