Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-25-2010, 10:05 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 60
racharambola5 is on a distinguished road
Default problem with cellForRowAtIndexPath in UITableView

Hi All, 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..

Code:
- (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...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....please guys help me...
racharambola5 is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone, memory management, objective-c, uitableview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 345
7 members and 338 guests
dre, freewind, hain, HemiMG, lendo, Newbie123, PlutoPrime
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,894
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:59 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0