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 8846111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:53:53+00:00 2026-06-14T11:53:53+00:00

I have a UITable that can collapse on header section click. I need the

  • 0

I have a UITable that can collapse on header section click.
I need the header sections to have unique text in them.

I can use cell.textlabel.text = @””; and this works fine, however I want to add more labels to the cell and position these i.e

            lbl123 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 20)];
            lbl123.font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];
            lbl123.textColor = [UIColor blackColor];
            lbl123.backgroundColor = [UIColor clearColor];
            lbl123.highlightedTextColor = [UIColor whiteColor];
            lbl123.opaque = NO;
            lbl123.adjustsFontSizeToFitWidth = TRUE;
            lbl123.numberOfLines = 0;
            lbl123.lineBreakMode = UILineBreakModeWordWrap;
            [cell addSubview:lbl123];
            lbl123.text = @"test";

but when I add this to the section on “cellforindexpath” It replicates itself on cells further down the table.

any ideas why this is happening??

full code below:-

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

    NSString *cellValue = 0;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:@"MyIdentifier"];
    }

    if (tableView.tag == 2)
    {
        UILabel *lbl123;
        if ([self tableView:tableView canCollapseSection:indexPath.section])
        {
            cell.textLabel.font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];

            if (!indexPath.row)
            {
                cell.textLabel.backgroundColor = [UIColor clearColor];
                UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
                [bgView setBackgroundColor:[UIColor blackColor]];
                [bgView setAlpha:0.3];
                [cell setBackgroundView:bgView];
                lbl123 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 20)];
                lbl123.font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];
                lbl123.textColor = [UIColor blackColor];
                lbl123.backgroundColor = [UIColor clearColor];
                lbl123.highlightedTextColor = [UIColor whiteColor];
                lbl123.opaque = NO;
                lbl123.adjustsFontSizeToFitWidth = TRUE;
                lbl123.numberOfLines = 0;
                lbl123.lineBreakMode = UILineBreakModeWordWrap;
                [cell addSubview:lbl123];
                lbl123.text = @"";

                if(indexPath.section == 1)
                {
                    cell.textLabel.font = [UIFont fontWithName:@"Trebuchet MS-Bold" size:15.0f];
                    cell.textLabel.text = [NSString stringWithFormat:@"My Results Overview:"];
                    cell.textLabel.textColor = [UIColor whiteColor];
                    cell.textLabel.numberOfLines = 3;
                    cell.textLabel.opaque = YES;

                    UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 294, 80)];
                    av.backgroundColor = [UIColor clearColor];
                    av.opaque = NO;
                    av.image = [UIImage imageNamed:@"Blue bar 720x115.png"];
                    cell.backgroundView = av;
                }

                if(indexPath.section == 2)
                {
                    cell.textLabel.font = [UIFont fontWithName:@"Trebuchet MS-Bold" size:15.0f];
                    cell.textLabel.text = [NSString stringWithFormat:@"Result1:"];
                    cell.textLabel.textColor = [UIColor blackColor];
                    cell.textLabel.numberOfLines = 3;
                    cell.textLabel.opaque = YES;


                    UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 294, 120)];
                    av.backgroundColor = [UIColor clearColor];
                    av.opaque = NO;
                    if ([confidenceCOL isEqualToString:@"Red"])
                    {
                        av.image = [UIImage imageNamed:@"redPanel.png"];
                    }
                    if ([confidenceCOL isEqualToString:@"Green"])
                    {
                        av.image = [UIImage imageNamed:@"greenPanel.png"];
                    }
                    if ([confidenceCOL isEqualToString:@"Purple"])
                    {
                        av.image = [UIImage imageNamed:@"purplePanel.png"];
                    }
                    cell.backgroundView = av;


                    lbl123.text = @"Test";
                }
                if(indexPath.section == 3)
                {
                    cell.textLabel.font = [UIFont fontWithName:@"Trebuchet MS-Bold" size:15.0f];
                    cell.textLabel.text = [NSString stringWithFormat:@"Result2:"];
                    cell.textLabel.textColor = [UIColor blackColor];
                    cell.textLabel.numberOfLines = 3;
                    cell.textLabel.opaque = YES;

                    UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 294, 120)];
                    av.backgroundColor = [UIColor clearColor];
                    av.opaque = NO;
                    if ([ambitionCOL isEqualToString:@"Red"])
                    {
                        av.image = [UIImage imageNamed:@"redPanel.png"];
                    }
                    if ([ambitionCOL isEqualToString:@"Green"])
                    {
                        av.image = [UIImage imageNamed:@"greenPanel.png"];
                    }
                    if ([ambitionCOL isEqualToString:@"Purple"])
                    {
                        av.image = [UIImage imageNamed:@"purplePanel.png"];
                    }
                    cell.backgroundView = av;

                    lbl123.text = @"TestING!";
                }

                if ([expandedSections containsIndex:indexPath.section]){}
                else{}

            }
            else
            {
                if(indexPath.section == 2)
                {
                    cellValue = Result1Moretext;
                }
                if(indexPath.section == 3)
                {
                    cellValue = Result2Moretext;
                }


                static NSUInteger const k1 = 1;

                // Declare references to the subviews which will display the data.
                UILabel *lbl1 = nil;

                static NSString *kCellID = @"CellID";

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

                    lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 300)];
                    lbl1.font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];
                    lbl1.tag = k1;
                    lbl1.textColor = [UIColor blackColor];
                    lbl1.backgroundColor = [UIColor clearColor];
                    lbl1.highlightedTextColor = [UIColor whiteColor];
                    lbl1.opaque = NO;
                    lbl1.adjustsFontSizeToFitWidth = TRUE;
                    lbl1.numberOfLines = 0;

                    lbl1.lineBreakMode = UILineBreakModeWordWrap;
                    [cell.contentView addSubview:lbl1];


                }else {
                    lbl1 = (UILabel *)[cell.contentView viewWithTag:k1];
                }

                cell.accessoryView.tag = indexPath.row;

                lbl1.text = [NSString stringWithFormat:@"%@", cellValue];
                return cell;

            }

        }



        else
        {

        }


    }

Thanks

  • 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-06-14T11:53:54+00:00Added an answer on June 14, 2026 at 11:53 am

    Don’t forget that cells are reused in UITableViews to keep memory usage low. So what’s happening is you are making a cell, and adding the label to it, then after that cell scrolls of the screen it goes into a reuse pool, gets re-added as a new cell further down and still has the label in it.

    What you should do is create all labels etc only when you make a new cell (in the cell==nil section, that’s where a cell was not dequeued), and set all the labels in the rest of the code.

    You can use view tags to find the label views again later.

    eg.

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:@"MyIdentifier"];
        lbl123 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 20)];
        lbl123.font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];
        lbl123.textColor = [UIColor blackColor];
        lbl123.backgroundColor = [UIColor clearColor];
        lbl123.highlightedTextColor = [UIColor whiteColor];
        lbl123.opaque = NO;
        lbl123.adjustsFontSizeToFitWidth = TRUE;
        lbl123.numberOfLines = 0;
        lbl123.lineBreakMode = UILineBreakModeWordWrap;
        [cell addSubview:lbl123];
        lbl123.text = @"";
        lbl123.tag=123;
    } else {
        lbl123 = (UILabel *)[cell viewWithTag:123];
    }
    
    if(indexPath.section == 2)
    {
        lbl123.text = @"Test";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently found that Matlab can use LaTeX character sequences. However, they only
I have a bunch of uiviews and uitable views. How can I theme them
I have a UITable that I am expanding to show more info text. Problem
I have a subview over my UITable that is used as a busy view.
I have a UITable View with a textfield that is editable right on the
I have been populating a UITable view cell with the data fetched from a
I have icons inside my application for uibutton & on uitable cell. Lets say
I have 3 objects in a cell. They are UILable with text (at left),
I have my uitable data being populated for the current section inside numberOfRowsInSection .
I have a UITable that has a UISearchDisplayController . The UITable is less than

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.