Flatron
Flatron

Reputation: 687

Padding on TableViewCell

I want to add padding to my table view cells. Thought I could do something like this but that didnt work.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath  *)indexPath
{
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height + 20;
}

Upvotes: 0

Views: 4139

Answers (3)

Bourne
Bourne

Reputation: 10312

See the below link. Explains the basics of getting a variable height (for cells) UITableView along with proper padding that your require. Example just includes text. Change it to suit your requirements within the cell.

http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/

Upvotes: 2

jrturton
jrturton

Reputation: 119242

When adding your cells you will need to set the y value of the cell's frame to half your padding to make it start at the right place. Also ensure that the cell does not have flexible height or it will expand to fill the height of the row.

Upvotes: 0

John Carter
John Carter

Reputation: 2056

Are you sure you placed this method in your delegate? it also depends on which cells. There are other methods...

// UITableViewDelegate Method
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
    {
    }

// UITableViewDelegate Method
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
    }

also, if you are using the contentView of the cell then you will need to change the frame of your custom view

Upvotes: 0

Related Questions