0
votes

I am using custom table view cell and my table view showing repeated data after index 5 I don't know why it is done When in ipad it shows after index 8 repeated data it may be indexing problem but couldn't find it below is the code - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"item";

ItemCell *cell = (ItemCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[ItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSLog(@"indexpath.row is %d",indexPath.row);

NSString *stre = [grouporphoto objectAtIndex:indexPath.row];
if([stre isEqualToString:@"Group"]){
    folderimage = YES;
    NSLog(@"here is folder");

    cell.item = [items objectAtIndex:indexPath.row];


}
else if([stre isEqualToString:@"Photo"]){
    folderimage = NO;
    NSLog(@"here is Photo");

    cell.item = [items objectAtIndex:indexPath.row];


}




return cell;
}

Please help Thanks in advance

2
Could it be that you never enter the ifs? The cell.item would never get updated and that would explain why you get duplicated data (reused cells). Put NSLog outside the ifs and see if it prints. - lawicko

2 Answers

1
votes

You should reset all your variables in another else-statement so that when your cell is reused the variables in them aren't.

else
{
    cell.item = nil;
}

This should fix it for you.

0
votes

It is because of the way iOS handles TableViews. As soon as a new cell comes into view (from being out of view i.e. below the bottom of the screen), it will ask for a new one, in which case it will try to reuse a cell that has moved out of the view.

Because of the reuse, not all instance variables are reset on the cell. You need to explicitly change everything you want to change. In other words, all pictures, text, labels etc etc that you want potentially different on each cell, set them explicitly each time.