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

  • Home
  • SEARCH
  • 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 3976034
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:45:23+00:00 2026-05-20T04:45:23+00:00

I am creating my cells from an array. The last object in my area

  • 0

I am creating my cells from an array. The last object in my area gets created differently, as I want to add a new UILabel to the contentView of the cell. (So that it shows a total number of records in a UILabel in the last cell).

For some reason, my last cell that adds the UILabel to the contentView keeps getting duplicated. It will show up on other cells, screw up their display, etc. I have a feeling this has to do with cell reuse, but I am not sure how to fix it.

Here is my method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *kCellID = @"cellID";

    // Bounds
    CGRect bounds = [[UIScreen mainScreen] bounds];

    Person *person = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        person = [people objectAtIndex:indexPath.row];
    }

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellID] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        // Check if it is the first cell
        if (person == [people lastObject]) {
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.userInteractionEnabled = NO;

            UILabel *count = [[UILabel alloc] initWithFrame:CGRectMake(-4, 11, bounds.size.width - 10, 20)];
            count.autoresizingMask = UIViewAutoresizingFlexibleWidth;
            count.textColor = [UIColor darkGrayColor];
            count.font = [UIFont systemFontOfSize:16];
            count.textAlignment = UITextAlignmentCenter;
            count.text = person.nameFirst;
            cell.textLabel.text = nil;
            cell.detailTextLabel.text = nil;
            [cell.contentView addSubview:count];

            return cell;
        }

    }//end

    cell.textLabel.text = [NSString stringWithFormat:@"%@, %@", person.nameLast, person.nameFirst];

    // Check if they are faculty staff
    if ([person.status isEqualToString:@"Staff/Faculty"]) {
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@: %@", person.status, person.department];
    } else {
        cell.detailTextLabel.text = person.status;
    }//end

    return cell;
}

Can someone help me understand how I can get this working correctly so that my UILabel doesn’t get created on different cells?

  • 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-20T04:45:24+00:00Added an answer on May 20, 2026 at 4:45 am

    I have adjusted your method below. Basically, you need to dequeue cells and perform any cell wide settings (disclosure, etc.) when you create the cell. Any individual cell changes should be done after the cell has been dequeued.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *kCellID = @"cellID";
        static NSString *lastCellID = @"lastcellID";
    
        // Bounds
        CGRect bounds = [[UIScreen mainScreen] bounds];
    
        Person *person = nil;
        if (tableView == self.searchDisplayController.searchResultsTableView) {
            person = [people objectAtIndex:indexPath.row];
        }
    
        UITableViewCell *cell;
    
        if (person != [people lastObject]) {
            cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
            if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellID] autorelease];
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
            cell.textLabel.text = [NSString stringWithFormat:@"%@, %@", person.nameLast, person.nameFirst];
    
            // Check if they are faculty staff
            if ([person.status isEqualToString:@"Staff/Faculty"]) {
                cell.detailTextLabel.text = [NSString stringWithFormat:@"%@: %@", person.status, person.department];
            } else {
                cell.detailTextLabel.text = person.status;
            }//end
        } else {
            cell = [tableView dequeueReusableCellWithIdentifier:lastCellID];
            if (cell == nil) {
               cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:lastCellID] autorelease];
               cell.accessoryType = UITableViewCellAccessoryNone;
               cell.userInteractionEnabled = NO;
    
                UILabel *count = [[UILabel alloc] initWithFrame:CGRectMake(-4, 11, bounds.size.width - 10, 20)];
                count.autoresizingMask = UIViewAutoresizingFlexibleWidth;
                count.textColor = [UIColor darkGrayColor];
                count.font = [UIFont systemFontOfSize:16];
                count.textAlignment = UITextAlignmentCenter;
                count.tag = 1;
                [cell.contentView addSubview:count];
            }
            UILabel *count = (UILabel *)[cell viewWithTag:1];
            count.text = person.nameFirst;
            //cell.textLabel.text = nil;
            //cell.detailTextLabel.text = nil;
        }//end
    
        return cell;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a uitableview with custom cells which I created in Interface Builder. There
What is the best way of creating and populating an Object from values passed
I'm loading my table from an XML feed in to an array then creating
I am creating an application where the user can select multiple cells from a
How would I go about creating an array containing each link title from the
I am creating an array like this: function imageSize($name, $nr, $category){ $path = 'ad_images/'.$category.'/'.$name.'.jpg';
I am creating a table based on result from servlet, which contains a checkbox
I'm creating an object hierarchy that is representing a table that is draw on
I have an events app which gets events data from sqlite database and displays
I have a command-line utility that gets quite a bit of downloads from my

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.