Custom UITableViewCell with 2 UILabel's resized

Posted by petert on Stack Overflow See other posts from Stack Overflow or by petert
Published on 2010-03-31T10:53:40Z Indexed on 2010/04/07 8:03 UTC
Read the original article Hit count: 530

I'm struggling to create a custom UITableViewCell (in IB) that has a UIImageView on the left, fixed to the top left corner, then 2 UILabel's adjacent to it, side by side. So the cell looks something like:

+---------------------------+
| IMAGE LABEL1    LABEL2  > |
+---------------------------+

What I cannot seem to do is to change the frame of LABEL1 so it fits the text is contains, then make sure LABEL2 moves to the left (or right) so it ends up directly adjacent to it, filling all the space for the rest of the cell.

So, concentrating on the label text, I'd like cell to look like this:

+---------------------------+
| IMAGE Call 123456       > |
+---------------------------+

after I've set LABEL1 to "Call" and LABEL2 to "123456" in:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath

Actually I can resize LABEL1 with this:

CGSize sizeToMakeLabel = [label.text sizeWithFont:label.font]; 

label.frame = CGRectMake(label.frame.origin.x,
                         label.frame.origin.y, 
                         sizeToMakeLabel.width + 3,
                         sizeToMakeLabel.height);

Now the frame for the text "Call" fits this text (confirmed by setting a background color), but the text "123456" doesn't seem to shift to the left, whatever autosizing I set in IB, or methods to resize the cells view? I seem to need the +3 to make sure "Call" didn't look like this "C…" also?

I'm using 2 labels so LABEL1 can be in a bold font say.

© Stack Overflow or respective owner

Related posts about uitableviewcell

Related posts about uilabel