我有一個要求用不同長度的環繞文本填充單元格。我目前的例程是處理這令人滿意的,但是,我擔心使用幾個常量的使用。如何處理iPhone代碼中的屏幕位置常量?
下面的值10和260表示預期的邊距和單元寬度,但它們僅對於縱向方向上的標準清晰度分辨率是準確的。
是否有一些屏幕/表格對象爲我提供這些值作爲我可以使用的指標而不是常量?
- (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;
}