user564963
user564963

Reputation: 2294

UILabel not refreshing in UITableViewCell when reloaded

I add two UILabels to a UITableViewCell and then name and number labels to that cell. When I reload the data in the UITableView (whether I add or delete a contact from the table view) the data in the labels are overlapping which means it's not reloading the labels, until the entire view is reloaded.

Can anyone please help?

Code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    UILabel *name;
    UILabel *number;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        //NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
        //      NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
        //      [sortDescriptor release];
    }
    CGRect Label1Frame = CGRectMake(10, 10, 140, 25);
    name = [[UILabel alloc] initWithFrame:Label1Frame];
    name.tag = 1;
    [name setBackgroundColor:[UIColor clearColor]];
    [cell.contentView addSubview:name];

    CGRect Label2Frame = CGRectMake(150, 10, 140, 25);
    number = [[UILabel alloc] initWithFrame:Label2Frame];
    number.tag = 1;
    [number setBackgroundColor:[UIColor clearColor]];
    [cell.contentView addSubview:number];

    name.text = [NSString stringWithFormat:@"%@",[names objectAtIndex:indexPath.row]];
    number.text = [NSString stringWithFormat:@"%@",[phonenumbers objectAtIndex:indexPath.row]];

    [name release];
    [number release];
    return cell;    
}

Upvotes: 1

Views: 3312

Answers (3)

Shyam Bhat
Shyam Bhat

Reputation: 1600

The cleanest way to do it is create custom table view cells and only set the label values in the CellForRowAtIndexPath method. That ensures the most efficient use and reuse of UITableViewCells.

Upvotes: 0

Andy Bowskill
Andy Bowskill

Reputation: 1734

You are not re-using your UITableViewCell objects correctly, you only need to allocate and setup your UILabel objects once per cell that needs to be created. If a cell does already exist and can be re-used then you just need to retrieve the desired UILabel objects and modify the text property for each:

I suggest reading the following:

UITableView class reference

Upvotes: 0

theChrisKent
theChrisKent

Reputation: 15099

You should move your label creation code inside the cell initialization and then just reference them later by tag like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";        

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        //NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
        //      NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
        //      [sortDescriptor release];
        CGRect Label1Frame = CGRectMake(10, 10, 140, 25);
        UILabel *name = [[UILabel alloc] initWithFrame:Label1Frame];
        name.tag = 1;
        [name setBackgroundColor:[UIColor clearColor]];
        [cell.contentView addSubview:name];
        [name release];

        CGRect Label2Frame = CGRectMake(150, 10, 140, 25);
        UILabel *number = [[UILabel alloc] initWithFrame:Label2Frame];
        number.tag = 2;
        [number setBackgroundColor:[UIColor clearColor]];
        [cell.contentView addSubview:number];
        [number release];
    }

    UILabel *nameLabel = (UILabel *)[cell viewWithTag:1];
    UILabel *numberLabel = (UILabel *)[cell viewWithTag:2];
    name.text = [NSString stringWithFormat:@"%@",[names objectAtIndex:indexPath.row]];
    number.text = [NSString stringWithFormat:@"%@",[phonenumbers objectAtIndex:indexPath.row]];

    return cell;    
}

Upvotes: 5

Related Questions