Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set margin for uitableviewcell

How to set the margin of UITableViewCell programmatically without creating a whole new custom cell ?

like image 258
JAHelia Avatar asked Dec 04 '11 13:12

JAHelia


3 Answers

You need to subclass UITableViewCell and override layoutSubviews method:

- (void)layoutSubviews {
    [super layoutSubviews];
    CGRect tmpFrame = self.imageView.frame;
    tmpFrame.origin.x += 10;
    self.imageView.frame = tmpFrame;

    tmpFrame = self.textLabel.frame;
    tmpFrame.origin.x += 10;
    self.textLabel.frame = tmpFrame;

    tmpFrame = self.detailTextLabel.frame;
    tmpFrame.origin.x += 10;
    self.detailTextLabel.frame = tmpFrame;
}
like image 156
Andrey Zverev Avatar answered Oct 12 '22 08:10

Andrey Zverev


Wouldn't it just make more sense to make the UITableViewCell taller than the content to begin with? If you're content is always 100px high , just make the cell 110px to get that extra 10px space you need, No custom cell needed :)

like image 26
Shai Mishali Avatar answered Oct 12 '22 09:10

Shai Mishali


To set the left margin you can use:

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:       (NSIndexPath *)indexPath
{
    return 1;
}
like image 1
fellowworldcitizen Avatar answered Oct 12 '22 08:10

fellowworldcitizen