0

How can I change the space between cells in tableviewcontroller?. I have an application by using Swift. I added a tableviewcontroller which is working fine. Now my problem is if I try to change the space between the cells it is not working. I have tried the following code.

 override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10; // space b/w cells
    }

Please help me to find what the issue is?

Edit 1:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        let a = 1;
        return a;
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let a = itemArray.count;
        return a;
    }
Amsheer
  • 7,046
  • 8
  • 47
  • 81

4 Answers4

2

To use the method you have to change the logic

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    let a = itemArray.count;
    return a;
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let a = 1;
    return a;
}

and have to change the rowAtIndexPath... too, using indexPath.section instead of indexPath.row, e.g.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell")

    cell.titleLabel.text = itemArray[indexPath.section] // if itemArray is [String]

    return cell
}
Drizztneko
  • 161
  • 7
  • I don't understand. What you are trying to say? – Amsheer Nov 26 '15 at 10:24
  • see my edit, this way is how you have to access to your array – Drizztneko Nov 26 '15 at 11:06
  • I am using this func. So what you are saying is I just want to remove first two override func in your answer and change from next right? – Amsheer Nov 26 '15 at 11:09
  • no, you have to use all functions. The table view needs all of them, the first for knowing the number of sections, the second for the number of rows in each section, and the last to populate each row. You also need the function you had write in your question to establish the space between them. – Drizztneko Nov 26 '15 at 11:12
1

With Swift 2 you can do spacing between UITableViewCells in this way:

// In this case I returning 140.0. You can change this value depending of your cell
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 140.0
}

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.contentView.backgroundColor = UIColor.clearColor()

    let whiteRoundedView : UIView = UIView(frame: CGRectMake(0, 10, self.view.frame.size.width, 120))

    whiteRoundedView.layer.backgroundColor = CGColorCreate(CGColorSpaceCreateDeviceRGB(), [1.0, 1.0, 1.0, 1.0])
    whiteRoundedView.layer.masksToBounds = false
    whiteRoundedView.layer.shadowOffset = CGSizeMake(-1, 1)

    cell.contentView.addSubview(whiteRoundedView)
    cell.contentView.sendSubviewToBack(whiteRoundedView)
}
Orkhan Alizade
  • 7,379
  • 14
  • 40
  • 79
  • look, you also can create your own custom cell. Later, make height equal to 120, add your elements to it and add margin 20, from bottom to your last element. It will looks, like you want – Orkhan Alizade Nov 26 '15 at 10:42
0

You can use layout margin function. This is only for iOS8. Subclass an UITableViewCell and

- (UIEdgeInsets)layoutMargins {
    return UIEdgeInsetsMake(10, 0, 10, 0);
}
cekisakurek
  • 2,474
  • 2
  • 17
  • 28
0

Hi the easiest way i found is simply change it FROM :

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

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let a = itemArray.count
        return a
    }

TO:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        let a = itemArray.count
        return a
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let a = 1
        return a
    }

AND ADD THIS :

override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return heightOfSpaceBetweenCells
    }
    
    override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footerV = UIView()
            footerV.backgroundColor = UIColor.clearColor()

            return footerV
    }
Ben Siso
  • 5
  • 2