What is the proper way to align UITableViewCells when only some have an imageView?

Posted by Topher Fangio on Stack Overflow See other posts from Stack Overflow or by Topher Fangio
Published on 2010-05-21T18:26:54Z Indexed on 2010/05/21 18:30 UTC
Read the original article Hit count: 389

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.

© Stack Overflow or respective owner

Related posts about iphone-sdk-3.0

Related posts about iphone-sdk-4.0