views:

151

answers:

1

Hello all,

I am new to iPhone programming and working on my first real application (i.e. one not written in a book or online) and I've run into a small problem which I could solve a multitude of ways, but feel like there should be a good solution that perhaps I am just missing.

Here is the scenario: I have a UITableView with a bunch of standard UITableViewCells in it. What I want to do is toggle a green check mark when the cell is selected and I have that part working (note: I'm already using the accessoryType for something else, so I can't use it for the checkmark...besides, it's not as pretty). Unfortunately, when I toggle the checkmark like so:

if (...) {
  cell.imageView.image = [UIImage imageNamed:@"checkmark.png"];
} else {
  cell.imageView.image = nil;
}

It makes the cell's label bounce back and forth depending on whether it is checked or not. What is the proper way to align the cell's text (set via cell.textLabel.text) regardless of whether or not it has an image set? The solutions I have come up with are:

  1. Create a blank 40x40 png image in Photoshop and set the unchecked to that
  2. Create a blank 40x40 image solely in code
  3. Set some setting that I don't know about that will align it for me
  4. Create a subclass of UITableCellView that does what I need (which would be stupid, I'd just go with option 1...)

Suggestions? Thoughts? Comments? Thank you very much :-)

P.S. I'd like the solution to work with OS 3.0 and 4.0 if that makes any sort of difference.

+1  A: 

Option 1 is the way to go for a standard cell.

Strictly speaking, you do not have to subclass UITableViewCell to customize the layout. You can add any views you wish to the contentView. So you can add a UILabel and UIImageView to the contentView instead of using the imageView and textLabel properties.

drawnonward
+1 Here is a relevant question http://stackoverflow.com/questions/1171476
Shaji
Thanks for the answer. @Shaji - Thanks for the comment, that question may help in the future.
Topher Fangio