views:

788

answers:

2

Similar to this previous question, I am having a problem with text alignment in my table cells. All the text is shifted up by a few pixels. I am not using custom cells; I am using regular UITableViewCells with the UITableViewCellStyleValue1 style, targeting iPhone OS 3.1. I would prefer a much simpler solution than the previous question's answer, especially since I'm not using custom cells. I'd also like to know exactly what the problem is, since that part of the question was never addressed.

Here's what it looks like on the simulator:

Simulator

And on the device:

Device

Edit: some more code as per request. (I'm building my table cells outside of cellForRowAtIndexPath.)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [cells objectAtIndex:[indexPath row]];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self loadCells];
    [table reloadData];

    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)loadCells
{
    cells = [[NSArray alloc] initWithObjects:[self aCell], [self bCell], nil];
}

- (UITableViewCell*)aCell
{
    UITableViewCell* cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Foo"] autorelease];
    cell.textLabel.text = @"A";
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (UITableViewCell*)bCell
{
    UITableViewCell* cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Foo"] autorelease];
    cell.textLabel.text = @"B";
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
A: 

Aside from doing the custom cell and taking full control, I would not rely on the simulator. The table rendering has to be extremely efficient and responsive to scrolling and Apple makes note that you could see graphical artifacts if you interfere with the built in efficiencies -- especially the cell-reuse. I have seen the simulator misbehave and my code works flawlessly on the device.

I would guess that you have assigned the text to be 'top aligned' within the font metrics. Were you goofing around with the IB's inspector %-3 (command-3) has the alignment for autosizing, etc.

mobibob
I'm not reusing cells. Nor am I "goofing around" with the cells in IB. I'm not making cells in IB, I'm creating cells programatically. See the code I posted.
Shaggy Frog
Sorry if you misunderstood my response. It was not to imply anything about your efforts, I just thought this was a good place to share my experience with IB and the simulator. I use the simulator a lot, but don't rely on it for any final look-and-feel. As far as 'goofing' I do it to try different things and often can't 'undo' as the permutations of IB are daunting. I did *not* mean anything personal or disparaging. Let me know how I can make it up to you and I will try my best. Very Sincerely - mobibob
mobibob
Shaggy Frog, I spent some time re-reading Apple's doc on TableView and I really think you need to be explicit in your declaration and assignments. Set the text alignments explicitly. I would say that the default of the device are the 'correct' values and the simulator is the one that is 'off' ... even though, visually, your simulator view looks better. I had to tweak my text frame in one of my apps to get it to look correct when I expected the defaults to be correct. Meanwhile .. it sounds like you got your answer .. sorry to have caused you any anxiety.
mobibob
+4  A: 

I know your code says you are using cell style UITableViewCellStyleValue1, but the device screenshot really looks like UITableViewCellStyleSubtitle.

You should set a value for cell.detailTextLabel.text to make sure you are getting the expected cell style.

On that note, if you aren't going to use the detailTextLabel property, you should probably use UITableViewCellStyleDefault.

benzado
Oops -- you're right. That seems to have fixed it! I had those lines in there, but commented them out, and then accidentally removed those commented-out lines when I was anonymizing the code for posting here. In any case, thanks very much! This is definitely a bug with the layout algorithm.
Shaggy Frog
Filed a bug report in Radar #7464950.
Shaggy Frog
What's the layout bug? If you *were* using the Subtitle style, then the text was being laid out correctly.
benzado
I was not using the subtitle style, as per my code above. The layout bug is the textLabel is only properly aligned if the detailTextLabel has been set, but they should be independent of each other.
Shaggy Frog
BTW - Can you comment on Apple's response? I am curious about their explanation? I looked for the RADAR #7464950, but it does not show up.
mobibob
There's no response so far. The bug remains "Open". I don't think you can look up other people's bug reports -- I mostly just posted the # for completeness, so I apologize for misleading you there. :)
Shaggy Frog
cooool! You saved me a lot. In some other posts, I saw ppl suggesting to create custom cells...
mshsayem
I forgot to update here: Apple said they have fixed the bug in iPhone OS 4.0.
Shaggy Frog
Shaggy Frog: I'm at WWDC '10 and I flipped when I saw my cell.textLabel misaligned (when it was fine before). Your one comment just saved me a LOT of grief. Voted up. Thank you!
Joe D'Andrea