2

If I create a background image for my table cell in the init method of my UITableViewCell subclass, the image comes out exactly how I drew it, and tableView:heightForRowAtIndexPath: simply adds a gap around it.

But if instead I create the background image in my tableView's tableView:cellForRowAtIndexPath: method and set it, changing the cell height will now stretch the image.

I need to create the background views in the cellForRowAtIndexPath method, as the background changes for different rows. How can I make my image's size fixed again?

Ric Levy
  • 966
  • 1
  • 15
  • 33

2 Answers2

5

Have you tried setting the contentMode to UIViewContentModeCenter for the backgroundView of the cell?

put it here

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
     cell.backgroundView.contentMode = UIViewContentModeCenter;
}
tsakoyan
  • 1,891
  • 14
  • 12
0

You could create something with:

UIView *cellBackView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
<insert image>;
cell.backgroundView = cellBackView;

My guess is the backgroundView gets strechted automatically, but you could make sure the image isn't stretched on that view by setting a width and height. This way, you can do whatever you like with that view.

Joetjah
  • 6,292
  • 8
  • 55
  • 90