Eugene Trapeznikov
Eugene Trapeznikov

Reputation: 3240

Reload TableViewCell's UILabels in dynamic table

I got app with UITableView.

There is UILabel in each UITableViewCell. I add labels in this way:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  //look at the upd
}

But when i try to reload data in this table with some other information, old infromation (old labels) dont disappear, but still on cells.

It's look like this: ...

How should i organized adding UILabels?

UPD

I correct code in this way:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Recent Dream";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

        UILabel *textLabel = [[UILabel alloc] init];
        textLabel.frame = CGRectMake(10, 0, self.view.frame.size.width -110, 48);
        textLabel.backgroundColor = [UIColor clearColor];
        textLabel.font = [UIFont boldSystemFontOfSize:16];
        [textLabel setTag:(int)indexPath];
        [cell.contentView addSubview:textLabel];


    }
    // Configure the cell...

    Dream *tempDream = [self.dreams objectAtIndex:indexPath.row];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM/dd/yyyy"];

    //Optionally for time zone converstions
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]];

    NSString *stringFromDate = [formatter stringFromDate:tempDream.dateAdd];

    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bodyWrapper.png"]];

    cell.contentView.backgroundColor = background;

    UILabel *textLabel = (UILabel *)[cell viewWithTag:(int)indexPath];
    textLabel.text = tempDream.title;

    NSLog(@"%@",tempDream.title);
    NSLog(@"%@",textLabel.text);

    cell.textLabel.text = @"";
    cell.detailTextLabel.text = stringFromDate;

    return cell;
}

And output look like this:

2012-02-20 15:58:10.512 DreamerIOS[3037:10403] lllooooonggg dreeeeeeeaaaaammmm yeeeeeeah
2012-02-20 15:58:10.513 DreamerIOS[3037:10403] (null)
2012-02-20 15:58:10.516 DreamerIOS[3037:10403] dream to end
2012-02-20 15:58:10.516 DreamerIOS[3037:10403] (null)
2012-02-20 15:58:10.519 DreamerIOS[3037:10403] adsfhadskgh dfagfadkuy gadyv dsfdfad fsdf a ghfncdhg hjfg f dfj ghf 
2012-02-20 15:58:10.519 DreamerIOS[3037:10403] (null)

So, NSLog(@"%@",tempDream.title); is OK, but NSLog(@"%@",textLabel.text); is null. Why?

Upvotes: 0

Views: 1145

Answers (2)

Hanon
Hanon

Reputation: 3927

It is because each time you add a new label to the cell.

When you init a new cell, you add the label and set a tag to it

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

    //  add label here
    ...
    [label setTag:999];
}

Then when you configure the cell, use [UIView viewWithTag:] to get the reference of the label

UILabel *label = (UILabel *)[cell.contentView viewWithTag:999];

update:

You should use a constant value for the tag. If you use the indexPath as the tag, the view may not found. Since you are reusing the cell, the system only create a certain number of cells. When the cell is out of view (not visible), dequeueReusableCellWithIdentifier will get the cell for you rather than create a new cell.

The reusableCell is a view that you create before. The case you use a dynamic value for the tag will like this: You are at cell 10(indexPath), the system find a reusableCell which is created at 3(indexPath). Since it was created at 3, the label tag is 3. But you find a view with tag 10, therefore the result is null.

Upvotes: 1

jay
jay

Reputation: 3667

It's because you are not properly reusing table view cell. You must create the labels in the following code block
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

Upvotes: 1

Related Questions