iosswiftspacinguitableviewcellpadding

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


Solution

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