如何在UITableViewCells之间添加间距 - Swift

Ujj*_*ani 5 uitableview ios swift

我有一个表有一些自定义.

我的tableView的照片

这是我的代码:

import UIKit

class ViewController: UIViewController, UITableViewDelegate {
    var exercises : [String] = ["Swimming", "Running", "Weight Lifting", "Biking", "Climbing"]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.
    }

    //Necessary for basic tableView setup. Defines number of rows in a specific section.
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        //Setting the amount of rows to the number of elements in exercises. This function returns that.
        tableView.backgroundColor = UIColor.clearColor()
        return exercises.count

    }

    //Necessary for basic tableView setup. Helps us out content for every cell in the index path. Runs = rows
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{



        tableView.separatorColor = UIColor.clearColor()

        //Setting the footer to default so the extra junk does not show
        tableView.tableFooterView = UIView()

        //This will be returned. This automatically creates a prototype cell
        var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

        //Setting every cell to the respective item in exercises
        cell.textLabel?.text = exercises[indexPath.row]
        cell.textLabel?.font = UIFont(name: "Avenir-Light", size: 17)
        cell.textLabel?.textColor = UIColor.whiteColor()
        cell.textLabel?.textAlignment = .Center

        //Border Code
        cell.layer.borderWidth = 2.0
        cell.layer.borderColor = UIColor.whiteColor().CGColor

        //Round Corners
        cell.layer.cornerRadius = 20




        return cell

    }
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        cell.backgroundColor = UIColor.clearColor()

    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
Run Code Online (Sandbox Code Playgroud)

我希望每个之间有一些间距UITableViewCell.我已经尝试过以下方法:

  1. 更改每行的高度.此选项不起作用,因为我有边框.添加更多高度只会使每一行看起来更大.

  2. 将每行转换为一个部分然后使用heightForHeader in section.帖子.我想避免这个选项,因为我必须将我的所有行转换为部分.

  3. 在每行中添加透明的UIView.同样,这个选项不起作用,因为我有边框.

还有其他选择吗?

谢谢

Ujj*_*ani 0

我尝试了 ozgur 的方法,但它不起作用,因为我的表视图单元格之间有边框。最终,我使用了这篇文章的答案。希望能帮助到你