views:

29

answers:

1

I am having a problem with my code skipping over the if(cell == nil) after about nine trips through the cellForRowAtIndexPath. Then items in my table start repeating and do so every nine items. When I remove the if(cell == nil) line, the table comes out beautifully, with all the data in the right order. However, if I scroll to the bottom of the table, my app crashes so that is not a good solution. Any ideas please??

Thank you!

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];     


    NSString *temp = [[views objectAtIndex:indexPath.row] objectForKey:@"racer"];
    NSString *val = [[views objectAtIndex:indexPath.row] objectForKey:@"pointsScored"];

    // Set up the cell...
    cell.textLabel.text = temp;
    cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
    cell.detailTextLabel.text = val;

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    [temp release];
    [val release];

    }

    return cell;
}
A: 

KLevSki,

That is because you are re-using tableview cells via dequeueReusableCellWithIdentifier which is a good thing on the iPhone platform. What happens is this:

1) Cell is created in the if (cell==nil) section

2) Once a number of cells are created (in your case 9 of them, based roughly on how many are shown on screen), the OS begins to re-use the table cells to be a good memory manager instead of creating a unique table cell for each and every row which could be memory intensive

3) Since the cell is being re-used, all you need to do in the section after the if (cell==nil) block is to update/change the information on each cell.

As an example... If you created a cell that had only an icon and a label on it, each time the cell is scrolled into view, you would update the icon and label to whatever image/string is appropriate for that cell.

For your case:

...

if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];     

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

}

// update cell
cell.textLabel.text = [[views objectAtIndex:indexPath.row] objectForKey:@"racer"];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
cell.detailTextLabel.text = [[views objectAtIndex:indexPath.row] objectForKey:@"pointsScored"];

return cell;
iWasRobbed
Woah! Thank you so much! That was the case! However, now when I run it and scroll down, I get this error:2010-07-12 15:48:05.753 TabTabTab[25374:20b] *** -[UICGColor isEqualToString:]: unrecognized selector sent to instance 0x3d9edc02010-07-12 15:48:05.755 TabTabTab[25374:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UICGColor isEqualToString:]: unrecognized selector sent to instance 0x3d9edc0'Any ideas??
Rossi
Never Mind! I removed the release lines and things are going well! Thank you so much for your help. I really appreciate it
Rossi
Not a problem, but please see my updated post. You can't just remove the `release` lines or you will leak those objects since they will never get released. Instead, just get rid of them since you don't need them anyways (see my code). Please also mark this question as answered by clicking the "check box" next to it
iWasRobbed
Thanks - I updated my code. I do not see the silly checkbox that says the question was answered though
Rossi
It is near the top of each "answer" by the up/down vote arrows. It just helps as a (strange) reward system for people answering questions. Best of luck
iWasRobbed