Objective-c – Right and left margin to UITableViewCell

iphoneobjective cuitableview

I created the following UITableViewCells and set the background to white.

enter image description here

(the black frame is the simulator frame).

I would like to add margins right and left to the cell.

I looked in many Q&A and they all suggested customizing the cell with UiImageView. Is there another way to do it?

Best Answer

add UIView as contentView of the cell:

in cellForRowAtIndexPath

UIView *viewLeft = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, heightOfTheCell)];
viewLeft.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:viewLeft];


UIView *viewRight = [[UIView alloc] initWithFrame:CGRectMake(310, 0, 10, heightOfTheCell)];
viewRight.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:viewRight];

and in this way you can have round corners:

[cell.layer setCornerRadius: 5];
[cell.layer setMasksToBounds:YES];
[cell.layer setBorderWidth:2.0f]
CGColorRef colorRed = [[UIColor redColor] CGColor];
[cell.layer setBorderColor:colorRed];

enter image description here