1

I'm new to iOS, I have an grouped table view, one section one row as below. How can I set the left and right margin for the cell?

Here is the code I set for the border...

[cell.layer setBorderColor:[UIColor colorWithRed:0.00 green:0.60 blue:1.00 alpha:1.0].CGColor];
[cell.layer setBorderWidth:1.0f];
[cell.layer setCornerRadius:5];

What I have now...

Expected result: enter image description here

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
yun
  • 125
  • 2
  • 11

2 Answers2

4

The best programming practise for this is subclassing your UITableViewCell and override its setFrame method.

- (void)setFrame:(CGRect)frame {
    frame.origin.x += 10;
    frame.size.width -= 20;
    [super setFrame:frame];
}

Also, you can set the corner radius and colour of the cell in drawRect method

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];

    // border radius
    [self.layer setCornerRadius:5.0f];

    // border
    [self.layer setBorderColor:[UIColor colorWithRed:0.00 green:0.60 blue:1.00 alpha:1.0].CGColor];
    [self.layer setBorderWidth:1.0f];
}

One more thing, if you just want to make the cell a little more attractive, add this method too in the drawRect:

[self.layer setShadowColor:[UIColor lightGrayColor].CGColor];
[self.layer setShadowOpacity:0.8];
[self.layer setShadowRadius:3.0];
[self.layer setShadowOffset:CGSizeMake(2.0, 2.0)];
Vincent Joy
  • 2,598
  • 15
  • 25
0

do like

Step -1

create the one Common UIView named as BackgroundView .and set subview to cell.contentView

Step -2

set the frame on BackgroundView whatever you need. based on contentview width

Step -3

add the Label, Date, Image to subview of BackgroundView.

Step -4

then set the layer for BackgroundView, final answer you will get

[cell.BackgroundView setBorderColor:[UIColor colorWithRed:0.00 green:0.60 blue:1.00 alpha:1.0].CGColor];
[cell.BackgroundView setBorderWidth:1.0f];
[cell.BackgroundView setCornerRadius:5];
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • Thanks for the steps, I have a question: actually I need content view and accessory view to be inside a same border. If set the border to the background view, is it also include the accessory view? – yun Nov 12 '15 at 11:28
  • @yun -- in your question is good, you can add like create one imageview add the image for functions, else see this link http://stackoverflow.com/questions/2876041/can-a-standard-accessory-view-be-in-a-different-position-within-a-uitableviewcel – Anbu.Karthik Nov 12 '15 at 11:31
  • Good idea! I just use an arrow image to replace accessory view – yun Nov 12 '15 at 11:44