TableView showing repeated data after some index

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


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.


It is because of the way iOS handles TableViews. As soon as a new cell comes into view (from being out of view ie 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.

链接地址: http://www.djcxy.com/p/60496.html

上一篇: 第一个UITableViewCell的辅助箭头没有显示

下一篇: TableView显示一些索引后重复的数据