views:

410

answers:

2

Hi,

I have this code below to populate my UITableView on the fly.

I have to display two kind of cells: a regular cell with a background image and a cell with a regular background image, plus a label and a button.

if Indexpath.row is less than a control variable, then regular cells are drawn. If not, cells with buttons and labels are drawn.

this is the code

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"MyIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

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


    UIImage *imageU;

    if (indexPath.row < controlVariable) { 

     imageU = [[[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] 
        pathForResource:[NSString stringWithFormat: @"table%d", indexPath.row] ofType:@"jpg"]] autorelease];

     cell.imageView.image = imageU;

    } else { 

     imageU = [[[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] 
          pathForResource:[NSString stringWithFormat: @"table-pg%d",numberX] 
                  ofType:@"jpg"]] autorelease];
     cell.imageView.image = imageU;



     NSString * myString = [NSString stringWithFormat: @"pago%d", numberX];
     UILabel * myLabel = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 49.0, 200.0, 22.0)];
     [myLabel setTextAlignment:UITextAlignmentLeft];
     [myLabel setBackgroundColor:[UIColor blueColor]];
     [myLabel setClipsToBounds:YES];
     [myLabel setFont:[UIFont systemFontOfSize:14.0]];
     [myLabel setTextColor:[UIColor blackColor]];
     [myLabel setText: myString];
     [myLabel setAlpha:0.6];
     [cell addSubview: myLabel];
     [myLabel release];


     UIButton *buyButton = [[UIButton alloc] initWithFrame:CGRectMake( 220, 4, 100, 35)];

     buyButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
     buyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

     [buyButton setTitle:NSLocalizedString(@"buyKey", @"") forState:UIControlStateNormal]; 
     [buyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
     buyButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];

     UIImage *newImage = [[[[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] 
           pathForResource: @"whiteButton" ofType:@"png"]] autorelease]
            stretchableImageWithLeftCapWidth:12.0f topCapHeight:0.0f];
     [buyButton setBackgroundImage:newImage forState:UIControlStateNormal];

     [buyButton addTarget:self action:@selector(comprar:) forControlEvents:UIControlEventTouchDown];

     buyButton.backgroundColor = [UIColor clearColor];

     [buyButton setTag:indexPath.row];
     [cell addSubview:buyButton];
     [buyButton release];

    }

    return cell;
}

The problem with this code is: when I scroll the UITableView down and reach the division between regular cells and cells with buttons and labels, I see it is rendering correctly, but if I go up after going deep down, I see the buttons and labels being added to cells that were not supposed to have them. From this point forward, all cells contains buttons and labels...

It is like the cells are not releasing its contents before drawing. It is like labels and buttons are being added on top of other buttons and labels already on the cell. Cells are not releasing its contents before drawing again.

How to solve that?

thanks for any help.

NOTE: I see barely no difference after making the changes suggested by the two first answers. Now, not all cells are wrong, just some. They change every time I scroll down the table and return to the beginning of the table.

+1  A: 

You should use two different reuseIdentifiers; one for cells with just images, and one for cells with images and buttons. The problem is that your one cell type is being reused, but its content is not (nor should it be) cleared out when it's dequeued.

Ben Gottlieb
+4  A: 

You should use a separate reuseIdentifier for each cell 'type' that you are using. In this case, you'll want to use two.

You'll also want to create/add the UILabel and UIButton when you get a dequeue miss and not for every run through.. In pseudocode:

UILabel * lbl;
UIButton * btn;
cell = [table dequeueReusableCellWithIdentifier:correctIdentifier];
if (cell == nil)
{
    cell = ...; // alloc cell
    lbl = ...;
    lbl.tag = kTagLabel;
    [cell addSubView:lbl];
    btn = ...;
    btn.tag = kTagButton;
    [cell addSubView:btn];
}
else
{
    lbl = (UILabel*)[cell viewWithTag:kTagLabel];
    btn = (UIButton*)[cell viewWithTag:kTagButton];
}

//... now set the text/image appropriately.

Otherwise, you create a label and button each time the cell is dequeued from the table. Scrolling up and down will cause lots of labels and buttons to be created that never get released.

Jason
thanks, that's it !!!!
Digital Robot