Badr Bujbara
Badr Bujbara

Reputation: 8691

Adding Spacing Between Basic Table View Cells

I have basic table view cells that I want to set some spacing between them.

enter image description here

I tried

cell.contentView.layoutMargins.top = 8

and

cell.layoutMargins.top = 8 

but that didn't work. I know I can create a custom cell, but I want to know how I can add margins this way.

EDIT:

Then I changed the cell style to custom:

enter image description here

and added a view so I can set up some constraints to it

enter image description here

but the results still the same, no spacing between cells, and it kinda ignored the custom view, i guess because it needs a custom class for the cell, but I don't want to do that.

enter image description here

Upvotes: 3

Views: 4806

Answers (5)

arsenius
arsenius

Reputation: 13276

In general, working with a UITableViewCell's or its contentView's layoutMargins is error prone. In my experience the layoutMargins of both the cell and the contentView get reset at several points during table reload. There are two workarounds:

  1. Use a spacing cell. For example, just create a transparent UITableViewCell subclass that has a specific height constraint set on the contentView.
  2. Use a transparent UITableViewCell subclass, and add your own (possibly opaque) subview to the contentView and set the top/bottom constraints to 1/2 your desired spacing. You can override setHighlighted:animated to control how the cell looks on selection. This is the method I'm using in the screenshots. It also works great for adding a margin on the left/right.

enter image description here enter image description here

Upvotes: 0

Anuraj
Anuraj

Reputation: 1240

If you have a single section try interchanging the number of rows in section and the number of the sections in tableView. Then for spacing in between you can use delegate:

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }

Upvotes: 0

alexburtnik
alexburtnik

Reputation: 7741

You should not add spacing between cells themselves, but rather add a custom containerView to cell's contentView with a top and/or bottom constraints.

enter image description here

Upvotes: 6

Sunil Prajapati
Sunil Prajapati

Reputation: 473

Please try it you can create header between two cell and add space

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return array.count
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return 1 // Because we need to space between two cell
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    //Configure your cell

    let cel:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell!
    cel.textLabel?.text = array[indexPath.section] //please use section because we have only one row for all section
    return cel
}

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat{
    return 15 // whatever you want space between two cell
}

Upvotes: 1

Ram
Ram

Reputation: 979

You achieve adding spacing between cells is to make numberOfSections = array.count and make each section contains only one row. And then define headerView and its height.

 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return cellSpacingHeight
    }

Upvotes: 0

Related Questions