簡體   English   中英

在Swift 3中的單元格之間添加空間

[英]Add space between cells in Swift 3

我想在UITableView的單元格之間添加空間。

這是我的代碼。 我的意思是細胞然后是空間,然后是細胞,然后是空間等等。

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

將此用於兩個單元格之間的空間

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)

如果您喜歡,只是簡單的方法...

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

自定義單元

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

描述

在此處輸入圖片說明

如果您的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