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;
}
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;