views:

20

answers:

1

I have a requirement to fill cells with varying length wraparound text. My current routine is handling this satisfactorily, however, I am concerned about the use of a couple of constants being used.

The values 10 and 260 below represent the margin and cell width expected, but they are only accurate for standard definition resolution in portrait orientation.

Is there some screen/table object that provides me these values as metrics I could use instead of the constants?

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

    UITableViewCell *cell;
    UILabel *lbl;
    CGRect frame;

    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero 
                                   reuseIdentifier:AnswerIdentifier] autorelease];

    frame.origin.x = 10;
    frame.origin.y = 10;
    frame.size.height = [self textHeight:indexPath];
    frame.size.width = 260;

    lbl = [[UILabel alloc] initWithFrame:frame];
    lbl.lineBreakMode = UILineBreakModeWordWrap;
    lbl.numberOfLines = 0;         
    [cell.contentView addSubview:lbl];
    [lbl release];

    return cell;

}

- (CGFloat)textHeight:(NSIndexPath *)indexPath {

    CGSize textSize;
    CGSize constraintSize;
    CGFloat height;

    NSString *theText = [self cellText:indexPath];

    constraintSize = CGSizeMake(260.0f, MAXFLOAT);

    textSize = [theText sizeWithFont:kCELL_FONT 
                   constrainedToSize:constraintSize 
                       lineBreakMode:UILineBreakModeWordWrap];
    height = textSize.height;

    return height;

}
A: 

The table obviously needs to fit in the frame of the containing view (which might be the top level view or the window), so I might use percentage or pixel offsets from that rect.

hotpaw2