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

The Archive Base Latest Questions

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

The tableView displayed below contains label . They are added as subView for each

  • 0

The tableView displayed below contains label. They are added as subView for each cell.

When I run my program for the first time, the cells that appear on the screen have the correct text. But when I scroll the table, the text in the labels begin to get mixed up. From what I understand the label of one cell is overlaid on top of another when the cell is created. This has been shown in the latter two screenshots. After each scroll, the different cells are overlaid on top of the other. (So there is no fixed pattern.) I tried to understand the problem by observing which cell which were going out of the screen and the ones which were entering. I tried to correlate this information with the labels on the cells, but I am not able to understand.

I request you to kindly help me understand the cause for this discrepancy.

Thanks you!

enter image description here

The relevant section of my code is as follows:

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

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

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    NSLog(@"s = %d, r = %d", indexPath.section, indexPath.row);

    if (indexPath.section == 0)
    {
        switch(indexPath.row)
        {
            case 0:
            [cell addSubview:classA];
            break;

            case 1:
            [cell addSubview:classB];
            break;

        case 2:
            [cell addSubview:classC];
            break;

        case 3:
            [cell addSubview:classD];
            break;

        case 4:
            [cell addSubview:classE];
            break;

        default:
            break;
    }
}

if(indexPath.section == 1)
{
    switch(indexPath.row)
    {
        case 0:
            [cell addSubview:classF];
            break;

        case 1:
            [cell addSubview:classG];
            break;

        case 2:
            [cell addSubview:classH];
            break;

        default:
            break;
    }
}

if(indexPath.section == 2)
{
    switch (indexPath.row)
    {
        case 0:
            [cell addSubview:classI];
            break;

        case 1:
            [cell addSubview:classJ];
            break;

        case 2:
            [cell addSubview:classK];
            break;

        default:
            break;
    }
}

return cell;
}
  • 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-14T14:53:24+00:00Added an answer on June 14, 2026 at 2:53 pm

    The reason is, you are using dequeueReusableCellWithIdentifier to reuse the cells. So when you are scrolling, the cells which are going out of visible area are getting reused for the current visible cells.

    Since you have already added labels like classB etc.. in these cells and not removed after that, and after that when you are adding labels such as classI etc.. on the same reused cells, it is getting drawn on top of this label. Since these labels have background color set as clear color, it will show the label which was drawn behind it also and hence giving an overlapping feeling.

    As per the screenshot, all the cells are looking identical and you can definitely reuse the cells with dequeueReusableCellWithIdentifier. So why dont you change your logic of creating the label outside cellForRowAtIndexPath and instead just use it as cell.TextLabel.text = @"Class B"; etc.. in the corresponding if conditions inside this method?

    Your code will look like,

    if (indexPath.section == 0)
        {
            switch(indexPath.row)
            {
                case 0:
                cell.textLabel = [self createLabelForRow:indexPath.row section:indexPath.section];
    //or if it is jut a simple label, you can just set font and other properties in cell.textLabel directly
                cell.textLabel.text = @"Class A";
                break;
    

    Update: Here is a not-so-good fix for dequeueReusableCellWithIdentifier reuse in case all cells are static. But this is not a recommended approach.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
      NSString *CellIdentifier = [NSString stringWithFormat:@"CellForRow%dSection%d",indexPath.row, indexPath.section];
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil)
       { 
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
       }
    
      //or try this
    
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
      if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
        }
    

    Update2: So if you are facing issues with above try this. Use my first approach and do the following.

    if (indexPath.section == 0)
        {
            switch(indexPath.row)
            {
                case 0:
                cell.textLabel = [self createLabelForRow:indexPath.row section:indexPath.section];
    
                // or do the following,
    
                [self removeAllTextFieldsFromCell:cell];
                UITextField *aTextField = [self createTextFieldForRow:indexPath.row section:indexPath.section];
                [cell.contentView addSubview:aTextField];
             }
    

    In removeAllTextFieldsFromCell: method use the R.A’s suggestion.

    For eg:-

    - (void)removeAllTextFieldsFromCell:(UITableViewCell *)cell {
    
        for (UITextField *textField in cell.contentViews.subViews) {
            [textField removeFromSuperView];
        }
    }
    

    Or just use,

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
    
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
        NSLog(@"s = %d, r = %d", indexPath.section, indexPath.row);
    
        //if it is all textfields in the cell, it will look like,
        [self removeAllTextFieldsFromCell:cell];
        UITextField *aTextField = [self createTextFieldForRow:indexPath.row section:indexPath.section];
        [cell.contentView addSubview:aTextField];
    
        return cell;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am loading a tableview. Each cell of my tableview should have varying heights
I have large images displayed in a grouped tableview. I would like the images
I am developing and tableView in which in each row I have to display
There are 3 cells in a tableview. I need to hide and display the
I have been experiencing strange behavior with the header drawing the first time. Here
I am trying to build a program where users data is displayed in table
I'm trying to create a custom UITableView with different cell identifiers. In the first
I'm using the setImageWithURL to lazily load images into my tableview cells but there
I am sorting my tableview by Distributor using the code below (it was Alphabetical
I want to display images in a TableView . I can display one image

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.