0

我对 UITableViewCell 中的 UILabel 有奇怪的问题。直到我拉下屏幕以使 UITableViewCell 位于顶部时它才会出现。这是该问题的视频。

http://youtu.be/apTJd1Y4RZk

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

    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;
4

2 回答 2

0

UILabel 的创建应该在单元格本身的创建中这是一个问题,我不知道它是否会解决您的错误,但它仍然是一个错误,请将您的代码更改为

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;

试试这个代码,看看它是否有效

于 2012-06-13T15:14:07.737 回答
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];
}
于 2012-06-13T15:54:14.777 回答