How to add spacing between UITableViewCell

saili picture saili · Jun 2, 2011 · Viewed 267.7k times · Source

Is there any way to add spacing between UITableViewCell?

I have created a table and each cell only contain an image. The image is assigned to the cell like this:

cell.imageView.image = [myImages objectAtIndex:indexPath.row];

but this make the image enlarged and fit into the whole cell, and there are no spacing between the images.

Or lets say in this way, the height of image are e.g. 50, and I want to add 20 spacing between the images. Is there any way to accomplish this?

Answer

Husam picture Husam · Mar 17, 2016

My easy solution using Swift :

// Inside UITableViewCell subclass

override func layoutSubviews() {
    super.layoutSubviews()

    contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
}

Result enter image description here