UILabel in UITableViewCell not drawing until cell is at top of screen

I have strange problem with UILabel in a UITableViewCell. It does not appear until I pull the screen down so that the UITableViewCell is at the top. Here is a video of the problem.

http://youtu.be/apTJd1Y4RZk

HEre is the code when the cell is created.

    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;

Answers


Creation of the UILabel should be inside the creation of the cell itself This is an issue, i dont know if it will resolve your bug, but it still is a bug, Please change your code to

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;

Try this code, and see if it works out


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];
}

Need Your Help

DB connection fails (System.Data.Sqlclient.SqlException)

c# sql-server ado.net system.data.sqlite sqlclient

I am programming a Weatherstation which gets the data from an API.

Monitor App Utilisation

iphone ios xcode app-store viewwillappear

Is there any way to track iPhone app utilisation? I'd like to know every time a user has opened or interacted with my app. I don't want any other information about the user or their device. I don't...