Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding space between cells in a section in swift 2

I need to add padding so space between each cell in each section in swift 2.1

but all I managed to do was adding header for section which I don't want that.

how can I add space between dynamic table cells?

Here is the code for adding header:

    // Set the spacing between sections
    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }

    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = UIView()
        header.backgroundColor = UIColor.clearColor()
        return header
    }

Here is creating table view code:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}


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


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell  {

    //Table view cells are reused and should be dequeued using a cell identifier.
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

    let rowData = self.tableData[indexPath.row]

    cell.textLabel!.text = rowData.name
    cell.textLabel!.textAlignment = .Right


    if let url  = NSURL(string: rowData.img),
        data = NSData(contentsOfURL: url)
    {
        cell.imageView?.image = UIImage(data: data)
    }



    return cell
    }

Right now my table view look like this:

enter image description here

update code and table view :

enter image description here

like image 298
anonymox Avatar asked Jan 06 '23 21:01

anonymox


1 Answers

There is no spacing between cells in a table view. Only in a collection view.

The options available to you are...

  1. Use a collection view instead. It's relatively easy to make it look like a table view. Using this you can use the inter item spacing.

  2. Increase the height of the cells by 2 pixels and add a blank area to the top or bottom of each. This will give the illusion of adding space between them.

like image 199
Fogmeister Avatar answered Jan 15 '23 04:01

Fogmeister