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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:18:09+00:00 2026-05-16T15:18:09+00:00

Table view cell management has driving me crazy from past two days. Please check

  • 0

Table view cell management has driving me crazy from past two days. Please check the code below and I will explain you the problem in detail..

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


    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    UILabel *nameLabel,*sugarLabel,*searchNameLabel,*searchSugarLabel;

    if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        CGFloat width = [UIScreen mainScreen].bounds.size.width - 50;
        CGFloat height = 20;
        CGRect frame = CGRectMake(10.0f, 10.0f, width, height);
        if(isSearchOn)
        {
            searchNameLabel = [[UILabel alloc] initWithFrame:frame];
            searchNameLabel.textColor = [UIColor blackColor];
            searchNameLabel.backgroundColor = [UIColor clearColor];
            searchNameLabel.textAlignment = UITextAlignmentLeft;
            searchNameLabel.font = [UIFont systemFontOfSize:14.0f];
            searchNameLabel.tag=260;
            [cell.contentView addSubview:searchNameLabel];
            [searchNameLabel release];

            searchSugarLabel= [[UILabel alloc] initWithFrame:frame];
            searchSugarLabel.textColor = [UIColor blackColor];
            searchSugarLabel.backgroundColor = [UIColor clearColor];
            searchSugarLabel.textAlignment = UITextAlignmentLeft;
            searchSugarLabel.font = [UIFont systemFontOfSize:14.0f];
            searchSugarLabel.tag=160;
            [searchSugarLabel setHidden:YES];
            [cell.contentView addSubview:searchSugarLabel];
            [searchSugarLabel release];
        }
        else{       
        nameLabel = [[UILabel alloc] initWithFrame:frame];
        nameLabel.textColor = [UIColor blackColor];
        nameLabel.backgroundColor = [UIColor clearColor];
        nameLabel.textAlignment = UITextAlignmentLeft;
        nameLabel.font = [UIFont systemFontOfSize:14.0f];
        nameLabel.tag=60;
        [cell.contentView addSubview:nameLabel];
        [nameLabel release];

        sugarLabel= [[UILabel alloc] initWithFrame:frame];
        sugarLabel.textColor = [UIColor blackColor];
        sugarLabel.backgroundColor = [UIColor clearColor];
        sugarLabel.textAlignment = UITextAlignmentLeft;
        sugarLabel.font = [UIFont systemFontOfSize:14.0f];
        sugarLabel.tag=160;
        [sugarLabel setHidden:YES];
        [cell.contentView addSubview:sugarLabel];
        [sugarLabel release];
        }
    }
    else {        

        if(isSearchOn)
        {
            searchNameLabel=(UILabel *)[cell.contentView viewWithTag:260];
            searchSugarLabel=(UILabel *)[cell.contentView viewWithTag:160];
        }
        else{
            nameLabel=(UILabel *)[cell.contentView viewWithTag:60];
            sugarLabel=(UILabel *)[cell.contentView viewWithTag:160];
        }

    }


    if (isSearchOn) { 

        cellValue = [searchResult objectAtIndex:indexPath.row]; 
        searchSugarLabel.text=cellValue.sugarId;
        NSString *searchText = [NSString stringWithFormat:@"%@ %@", cellValue.firstName, cellValue.lastName];
        searchNameLabel.text=searchText;
        NSLog(@"%@",searchNameLabel.text);
        NSLog(@"%@",searchSugarLabel.text);
    }


     else {

            NSString *contact=[contactKeys objectAtIndex:[indexPath section]];
            NSArray *contactSection=[contactNames objectForKey:contact];
            sugar=[db getSugarId:@"Contacts" bySection:contact andIndex:indexPath.row];


            NSString *cellText = [contactSection objectAtIndex:[indexPath row]];

            // split the text by the : to get an array containing { "AAA", "BBB" }
            NSArray *splitText = [cellText componentsSeparatedByString:@":"];

            // form a new string of the form "BBB AAA" by using the individual entries in the array
            NSString *contactText = [NSString stringWithFormat:@"%@ %@", [splitText objectAtIndex:1], [splitText objectAtIndex:0]];
            nameLabel.text = contactText;
            sugarLabel.text = sugar;
}
    return cell;
}

Contacts is a class which has the properties firstName,lastName and sugar id in it..I am assigning the properties of a contacts class to the variables in the database method and returning an array of contact objects. searchResult is now an array of contact objects.The problem is when I logged the contents on the console the database gets everything in it and returns an array of contacts.The contacts in the searchResult points to different memory locations but when I try to debug the cellForRowAtIndexPath method after it gets 6 contacts..the 7th contact points to the same memory location as the 1st and it repeats thus in the searchNameLabel.text it returns a null and indexPath points to nil…I think it is cell re use issue and I accept that I am very bad in that..I need to figure this out as I am going to finish off my project with this…please guys help me…

This happens only when I try to search for the contacts. It works fine when I try to load all the contacts onto 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-16T15:18:09+00:00Added an answer on May 16, 2026 at 3:18 pm

    You are initializing your subviews in the if(cell== nil) block, but in the corresponding else-block, you overwrite them again.

    You should rethink your design: Do not load different views by searchon, but set their properties depending on searchon

    if(cell == nil){
       //do all initializing
    }
    
    if(searchon){
        //set view/label properties for searching style
    } else {
        //set view/label properties for not-searching style
    }
    

    another approach could be to have totally separated NIB files for the searchon/!searchon

    if(searchon){
        static NSString *SearchOnCellIdentifier = @"SearchOnCell"; 
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SearchOnCellIdentifier];
        if (cell == nil){
            //load cell from extra nib
        }
    
    } else {
        static NSString *SearchOFFCellIdentifier = @"SearchOFFCell"; 
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SearchOFFCellIdentifier];
        if (cell == nil){
            //load cell from extra nib
        }
    }
    

    NOTE: I never did that and it is not tested.

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

Sidebar

Related Questions

I have a UI Table View Controller. Each Cell Loads an image from my
Table view cell imageView is working on the simulator but not on the device..
I am using a Table View Cell to draw custom cell in table view.
I have implemented a custom table view cell. I can get the cell to
I have a grouped table view with textfields in the tableview. For the keyboard
In my iPhone application I have a table view. When user taps any row,
Here's my case: I have a table view showing contacts. Add button in the
I'm writing an iOS app with a table view inside a tab view. In
This is what i'm trying to achieve: Home (Table View) > (Click on row)
It's easy enough to set up a table view to allow editing. Just add

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.