UITabel在UITableViewCell中不会绘制,直到单元格位于屏幕顶部

时间:2012-06-13 15:04:41

标签: iphone objective-c ios ios5

我在UITableViewCell中遇到了UILabel的奇怪问题。直到我拉下屏幕以便UITableViewCell位于顶部才会出现。这是一个问题的视频。

http://youtu.be/apTJd1Y4RZk

HEre是创建单元格时的代码。

    TVC_Location_View_Text *cell = (TVC_Location_View_Text *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TVC_Location_View_Text" owner:nil options: nil];

        for(id currentObject in topLevelObjects) {
            if([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = ((TVC_Location_View_Text *) currentObject);
                break;
            }

        }

    }

    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:15.0];
    CGSize constraintSize = CGSizeMake(300.0f, MAXFLOAT);
    CGSize labelSize = [self.sLocationText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
    CGRect rect = CGRectMake(10, 10,  labelSize.width, labelSize.height);

    UILabel* labelText = [[UILabel alloc] initWithFrame:rect];
    [labelText setFont:cellFont];
    [labelText setLineBreakMode:UILineBreakModeWordWrap];
    [labelText setNumberOfLines:0];
    [labelText setBackgroundColor:[UIColor clearColor]];
    [labelText setTextColor:appDelegate.colorDarkText];

    [cell addSubview:labelText];

    [labelText setText:self.sLocationText];


    return cell;

2 个答案:

答案 0 :(得分:0)

UILabel的创建应该在细胞本身的创建中 这是一个问题,我不知道它是否能解决你的bug,但它仍然是一个bug, 请将您的代码更改为

TVC_Location_View_Text *cell = (TVC_Location_View_Text *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel* labelText;
if (cell == nil) {

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TVC_Location_View_Text" owner:nil options: nil];

    for(id currentObject in topLevelObjects) {
        if([currentObject isKindOfClass:[UITableViewCell class]]) {
            cell = ((TVC_Location_View_Text *) currentObject);
            break;
        }

    }

    labelText = [[UILabel alloc] initWithFrame:rect];
    [labelText setFont:cellFont];
    [labelText setLineBreakMode:UILineBreakModeWordWrap];
    [labelText setNumberOfLines:0];
    [labelText setBackgroundColor:[UIColor clearColor]];
    [labelText setTextColor:appDelegate.colorDarkText];
    labelText.tag = 111;
    [cell addSubview:labelText];
}

labelText = (UILabel*)[cell viewWithTag:111];

UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:15.0];
CGSize constraintSize = CGSizeMake(300.0f, MAXFLOAT);
CGSize labelSize = [self.sLocationText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
CGRect rect = CGRectMake(10, 10,  labelSize.width, labelSize.height);

[labelText setText:self.sLocationText];


return cell;

试试这段代码,看看它是否有用

答案 1 :(得分:0)

You should use -[UITableViewDelegate willDisplayCell:forRowAtIndexPath:] delegate method.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TVC_Location_View_Text *cell = (TVC_Location_View_Text *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel* labelText;
    if (cell == nil) 
    {

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TVC_Location_View_Text" owner:nil options: nil];

        for(id currentObject in topLevelObjects)
        {
                if([currentObject isKindOfClass:[UITableViewCell class]])
            {
                  cell = ((TVC_Location_View_Text *) currentObject);
                   break;
                }

        }

     labelText = [[UILabel alloc] initWithFrame:rect];
     [labelText setFont:cellFont];
     [labelText setLineBreakMode:UILineBreakModeWordWrap];
     [labelText setNumberOfLines:0];
     [labelText setBackgroundColor:[UIColor clearColor]];
     [labelText setTextColor:appDelegate.colorDarkText];
     labelText.tag = 111;
     [cell addSubview:labelText];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    UILabel* labelText = (UILabel*)[cell viewWithTag:111];

    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:15.0];
    CGSize constraintSize = CGSizeMake(300.0f, MAXFLOAT);
    CGSize labelSize = [self.sLocationText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
    CGRect rect = CGRectMake(10, 10,  labelSize.width, labelSize.height);

    [labelText setText:self.sLocationText];
}