Adding space between cells in a section in swift 2

7.3k Views Asked by At

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

6

There are 6 best solutions below

10
ChikabuZ On BEST ANSWER

You should just set cell height:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
   return 100 //cell height
}

UPDATE:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
   if indexPath.row % 2 == 0
   {
      return 100 //cell height
   }
   else
   {
      return 5 //space heigh
   }
}

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)

    if indexPath.row % 2 == 0
    {
        let rowData = self.tableData[indexPath.row/2]

        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)
        }
        else
        {
            cell.imageView?.image = nil
        }
    }
    else
    {
        cell.textLabel!.text = nil
        cell.imageView?.image = nil
    }
    return cell
}
9
Fogmeister On

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.

2
Muhammad Saifullah On

There are 2 ways to increase the height

  1. in storyboard select you you tableview and set the rowHeight(which better suites you implementation) in size inspector

  2. Add this function in your viewController

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
     { return 35;//Whatever height you want...}
    
0
Ananth On

You can Use the HeightForRowAtIndexPath or change the "row height" in your tableView Xib under the size Inspector...

Or you can check this link for objective-c: https://stackoverflow.com/a/14726457/4489420... In this it will create sections based on your array count...

0
Ananth On

Check this one if you are using Dynamic Heights... https://www.facebook.com/iOSApplicationDevelopers/posts/1297809426911662

2
Nicolas Miari On

Table view cells are contiguous by design: There is no "space" between two consecutive cells; only the 1 point thick separator line (which you can do away with).

  • You can add "padding" ( = internal space) inside each cell (i.e., make them taller - see other answers), but
  • You can not add "margin" ( = external space) between them (that would give you two separators between each cell, perhaps...?).

Collection view cells, on the other hand, do allow for extra space between cells.