Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 415701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:24:25+00:00 2026-05-12T18:24:25+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T18:24:26+00:00Added an answer on May 12, 2026 at 6:24 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 275k
  • Answers 275k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer select.insert(new Element('option', {value: myValue}).update(myLabel)); insert appends to the content of… May 13, 2026 at 2:35 pm
  • Editorial Team
    Editorial Team added an answer Use the following regular expression: ^\d{4}-\d{2}-\d{2}$ as in if (str.matches("\\d{4}-\\d{2}-\\d{2}"))… May 13, 2026 at 2:35 pm
  • Editorial Team
    Editorial Team added an answer I think you should use some GUI event to detect… May 13, 2026 at 2:35 pm

Related Questions

I'm trying to populate a UITableview with an array of cells. I usually do
I am cleaning up my code in a phonebook iPhone application and the Leaks
I'm getting XSS errors when trying to consume a .NET web service and I'm
I am working on a comment script using ajax, json and jquery. I have

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.