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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:35:25+00:00 2026-06-02T12:35:25+00:00

I have been trying to figure this out for a bit. I create a

  • 0

I have been trying to figure this out for a bit. I create a custom cell in its own xib file. In my view controller I have setup a table view controller with sections. The data that is being pulled into the table View is based off a fetch request controller from some core data that I have. I set up the custom cell in the cellForRowAtIndexPath function. I am creating a label for each cell within this function and populating the label with some data from the managed object. Everything seems ok when I first run. However, when I try to scroll up and down and new cells are reused the data in the labels are placed in the wrong cells. I have seen and heard this has to do with the reuse of cells. However, have not seen much examples on correcting this issue. Below is some of the code I have in my cellForRowAtIndexPath function. Let me know if any other input may be needed. Thanks for any help.

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

    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:@"CustomCell"];

    /* do this to get unique value per cell due to sections. */
    NSInteger indexForCell = indexPath.section * 1000 + indexPath.row + 1;

    NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];



    NSString *lastSession = nil;
    UILabel *lastSessionLabel = nil;
    if(cell == nil) {

        lastSession = [managedObject valueForKey:@"last_session"];

        [self.tableView registerNib:[UINib nibWithNibName:@"CustomCell" 
                                                   bundle:[NSBundle mainBundle]] 
             forCellReuseIdentifier:@"CustomCell"];

        self.tableView.backgroundColor = [UIColor clearColor];
        cell = [aTableView dequeueReusableCellWithIdentifier:@"CustomCell"];

        lastSessionLabel = [[UILabel alloc]initWithFrame:CGRectMake(410,55, 89, 35)];
        lastSessionLabel.textAlignment = UITextAlignmentLeft;
        lastSessionLabel.tag = indexForCell;
        lastSessionLabel.font = [UIFont systemFontOfSize:17];
        lastSessionLabel.highlighted = NO;
        lastSessionLabel.backgroundColor = [UIColor clearColor];

        cell.contentView.tag = indexForCell;
        [cell.contentView addSubview:lastSessionLabel];
    } else {
        lastSessionLabel = (UILabel *)[cell viewWithTag:indexForCell];
    }

        if (lastSession && lastSession.length) {        
            lastSessionLabel.text = lastSession;
        } 


    cell.textLabel.text = [NSString stringWithFormat:@"%@%@%@%@", @"Dr. ",
                           [managedObject valueForKey:@"first_name"], 
                           @" " , 
                           [managedObject valueForKey:@"last_name"]];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.editingAccessoryType = UITableViewCellAccessoryNone;


    return cell;
}

** Revised Code **

Below are the changes to code: in viewDidLoad is the following:

 - (void)viewDidLoad
{
    [super viewDidLoad];
   [self.tableView registerNib:[UINib nibWithNibName:@"CustomCell" 
                                           bundle:[NSBundle mainBundle]] 
         forCellReuseIdentifier:@"CustomCell"];

       self.tableView.backgroundColor = [UIColor clearColor];
  }     

 in -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath {
  UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:@"CustomCell"];
    NSInteger indexForCell = indexPath.section * 1000 + indexPath.row + 1;
    NSLog(@"index for cell: %d",indexForCell);

    NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];


    NSString *lastSession = [managedObject valueForKey:@"last_session"];
 UILabel *lastSessionLabel = nil;
    if(cell == nil) {
        NSLog(@"Cell is nil! %@", [managedObject valueForKey:@"first_name"]);

         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"];


        self.tableView.backgroundColor = [UIColor clearColor];
   }    
        lastSessionLabel = [[UILabel alloc]initWithFrame:CGRectMake(410,55, 89, 35)];
        lastSessionLabel.textAlignment = UITextAlignmentLeft;
        lastSessionLabel.tag = indexForCell;
        lastSessionLabel.font = [UIFont systemFontOfSize:17];
        lastSessionLabel.highlighted = NO;
        lastSessionLabel.backgroundColor = [UIColor clearColor];


        [cell.contentView addSubview:lastSessionLabel];
        /* Appropriate verbiage for nil last session. */
       if (lastSession && lastSession.length) {        
            lastSessionLabel.text = lastSession;
        }   
 return cell;  
}

 I am still having issues again with the label cell text changing when I scroll for different cells. I read some where about maybe having to use the prepareForReuse function for this.
  • 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-02T12:35:28+00:00Added an answer on June 2, 2026 at 12:35 pm

    You are only fetching lastSession when you create a new cell. Try putting this line before the if(cell == nil) statement.

    lastSession = [managedObject valueForKey:@"last_session"];
    

    I.e. this:

    NSString *lastSession = [managedObject valueForKey:@"last_session"];
    

    in stead of this:

    NSString *lastSession = nil;
    

    UPDATE

    You are also setting the same tag for two views:

    lastSessionLabel.tag = indexForCell;
    ...
    cell.contentView.tag = indexForCell;
    

    Based on your code sample you should only use the first line, i.e. set the tag for the lastSessionLabel

    SECOND UPDATE

    You should also only call registerNib: once in your view lifecycle, e.g. in viewDidLoad, not every time you need a new cell. Furthermore, you should create a new cell if cell == nil in stead of using dequeueReusableCellWithIdentifier:. E.g.

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to figure this out for way to long tonight. I
I have been trying to figure this out for some time. It seems lots
I have been pulling my hair trying to figure this out but nothing is
I have been scratching my head for hours trying to figure out why this
I have been trying to figure out how to cancel a file upload to
Been sitting here for hours now trying to figure this out, so a bit
I have been doing a bit of research trying to figure out if i
I've been trying to figure this one out for a bit... I even downloaded
For quite a bit of time now I have been trying to figure out
hey people I'm new at jquery and i have been trying to figure this

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.