2010-09-06 54 views
0

我有一個要求用不同長度的環繞文本填充單元格。我目前的例程是處理這令人滿意的,但是,我擔心使用幾個常量的使用。如何處理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; 

} 

回答

0

表顯然需要適合在含有視圖(這可能是頂級視圖或窗口)的幀,所以可以使用百分比或像素偏移從該矩形。

0

嘗試使用

[[UIScreen mainScreen] applicationBounds]; 

這將返回一個CGRect當前窗口的大小。然後,對於您的代碼,您可以使用:

CGRect screenRect = [[UIScreen mainScreen] applicationBounds]; 
frame.origin.x = 10; 
frame.origin.y = 10; 
frame.size.height = [self textHeight:indexPath]; 
frame.size.width = screenRect.size.width - 60.0; 

假設x中的像素偏移是固定的,並且具有靈活的標籤寬度。