0

I want to add space between the cells that I have in my 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
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Q8i Code
  • 9
  • 1
  • 3

3 Answers3

1

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)
Pang
  • 9,564
  • 146
  • 81
  • 122
Manvir Singh
  • 129
  • 2
  • 11
0

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

description

enter image description here

Damyan Todorov
  • 536
  • 1
  • 6
  • 17
-1

If you have a vertical scroll for your UITableViewController or UICollectionViewController you need to add this code:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 10
}
RobC
  • 22,977
  • 20
  • 73
  • 80
Manu
  • 629
  • 7
  • 6