views:

1171

answers:

3

For some reason, I can not seem to change the default position of an image in a grouped tableview cell.

In the cellForRowAtIndexPath method I use the following to load the image:

cell.imageView.image = [UIImage imageWithContentsOfFile:MainImagePath];

I've been experimenting with the following to adjust the position of the image but have had no luck.

cell.contentMode = UIViewContentModeCenter;

I've also tried this with no luck.

cell.imageView.contentMode = UIViewContentModeCenter;
+2  A: 

Can you use the frame of the imageView ie: cell.imageView.frame = CGFrameMake(0.0, 0.0, 20.0, 20.0);

or set the center of the imageView with

cell.imageView.center = CGPointMake (20.0, 10.0);

=Seth

EDITED: Thanks Jonah, I fixed that!

MagicSeth
Thanks! I think you mean CGPointMake though, not CGMakePoint.
Jonah
+1  A: 

To move the imageView, you need to change it's frame. The things you're doing are just reconfiguring how it responds to view resizing. Try setting cell.imageView.center to the new center you want.

You should be aware of tableView:willDisplayCell:forRowAtIndexPath:. This is called immediately before displaying the cell, and is your last (and often best) chance to establish layout. tableView:cellForRowAtIndexPath: is called much earlier and its results are cached. So the layout may be wrong by the time it's actually displayed (due to view resizing for instance). That's why we generally do layout in tableView:willDisplayCell:forRowAtIndexPath:.

Rob Napier
Can you recommend how to set the cell.imageView.center as you recommend? The method described below by Seth causes an "incompatible type for argument" error.Thanks!
Jonah
Oops, seth's code had a small typo, and I got it to build, but it still seems to be ignored.
Jonah
You're setting it in tableView:willDisplayCell:forRowAtIndexPath:? Post the code.
Rob Napier
+1  A: 

The default layout of UITableViewCell is [imageView][textLabel][accessoryView]. You can't change that.

If you want to arbitrarily position an image in your UITableViewCell you must add a UIImageView to the cell's contentView.

Darren
Perhaps you're thinking of OS2.2, when the layout was somewhat magical? OS3 definitely gives you access to the underlying UIView pointers, which is specifically so you can treat them as normal views and modify them accordingly, including moving them around. Check the other answers to this question of examples.
Rob Napier
Since UITableViewCell is responsible for the layout of those views, changing their position seems to me to be a hack. It may work, but I don't see any documentation that guarantees that UITableViewCell won't reset its layout, say, when transitioning between edit and non-edit states. On the other hand, Apple's documentation does recommend adding subviews to the cell's contentView, in which case you can guarantee that you are solely responsible for its layout.
Darren
The layout concern is why Apple gave us tableView:willDisplayCell:forRowAtIndexPath:, to give us an opportunity, in a supported way, to perform layout and updates before display. If you have access to the WWDC videos, "Perfecting Your iPhone Table Views" had good discussion of this topic.
Rob Napier