【问题标题】:Add space between cells in Swift 3在 Swift 3 中的单元格之间添加空格
【发布时间】:2017-04-15 07:35:31
【问题描述】:

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

【问题讨论】:

标签: ios swift uitableview


【解决方案1】:

将其用于两个单元格之间的空间

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)

【讨论】:

    【解决方案2】:

    如果你喜欢的话,只是简单的方法......

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

    【讨论】:

      【解决方案3】:

      如果您的UITableViewControllerUICollectionViewController 有垂直滚动条,则需要添加此代码:

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

      【讨论】: