Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - add horizontal padding to tableview cells

enter image description here

I want to add horizontal padding for my table view cells as the image shows.

I created a subclass of UITableViewCell, whose width was 314 pixels (screen's width is 320), and set its frame in the initWithCoder method:

[self customizeXPosition:self.profileImage];
[self customizeXPosition:self.birthdayLabel];
[self customizeXPosition:self.heightLabel];
[self customizeXPosition:self.weightLabel];

this is my - (void)customizeXPosition:(UIView *)view:

- (void)customizeXPosition:(UIView *)view {
    CGRect tmpRect = view.frame;
    tmpRect.origin.x += 3.0;
    view.frame = tmpRect;
}

The custom cell was smaller than screen, and I moved every element in the cell to right by 3 pixels. I thought this code should achieve my goal, yet it didn't. The view didn't change in the simulator, just like I didn't write any code.

how can I achieve my goal?

like image 538
Brian Avatar asked Oct 29 '13 03:10

Brian


1 Answers

Try overriding the -setFrame method in UITableViewCell as per this post :

How to set the width of a cell in a UITableView in grouped style

Swift version

like image 112
faterpig Avatar answered Oct 13 '22 02:10

faterpig