2012-01-31 11 views
1

私はUITableViewCellでカスタムUILabelを表示しようとしていますが、何か間違っています。 マイコード:結果のUITableViewCell addSubviewとCGRectMakeの問題

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSDictionary *currentComment = [comments objectAtIndex:indexPath.row]; 

    static NSString *CellIdentifier = @"TitleCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    int commentLevel = [[currentComment objectForKey:@"level"] intValue]; 
    NSLog(@"Comment level: %i", commentLevel); 
    NSString *commentText = [currentComment objectForKey:@"text"]; 
    UILabel *titleLabel = [[UILabel alloc] init]; 
    titleLabel.numberOfLines = 0; 
    [titleLabel setFont:[UIFont fontWithName:@"Verdana" size:17.0]]; 
    CGSize textSize; 
    if (commentLevel == 0) { 
     textSize = [commentText sizeWithFont:titleLabel.font constrainedToSize:CGSizeMake(310, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap]; 
     //titleLabel.bounds = CGRectMake(5, 5, textSize.width, textSize.height); 
    } else { 
     textSize = [commentText sizeWithFont:titleLabel.font constrainedToSize:CGSizeMake((295-10*commentLevel), FLT_MAX) lineBreakMode:UILineBreakModeWordWrap]; 
     //titleLabel.bounds = CGRectMake(15, 5, textSize.width, textSize.height); 
    } 
    titleLabel.bounds = CGRectMake(20, 20, 300, 100); 
    titleLabel.text = commentText; 
    [cell.contentView addSubview:titleLabel]; 

    return cell; 
} 

スクリーンショット:enter image description here

答えて

2

あなたがラベルのテキストにガベージを設定しているかのように表示されるので、コメント値を取得していることを確認してください。

また、UITableViewCellに関連する場所にするため、ラベルの枠を枠の代わりに設定する必要があります。

コードを変更してください。 titleLabel.bounds = CGRectMake(20、20、300、100); 〜 titleLabel.frame = CGRectMake(20、20、300、100);

2

あなたはtitleLabel.frameないtitleLabel.boundsを設定する必要があります。境界線は位置とは関係ありません。

関連する問題