I have a UITableView which loads data from XML using the below code. It loads the information correctly, but the final two cells from the array are a repeat of the first two cells. I don’t know if I’m loading it incorrectly; I NSLog out the values as I build the array and they are correct. I’ve also logged the final array and that’s fine.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
ViewRoutesCell * aCell = (ViewRoutesCell *)[tableView dequeueReusableCellWithIdentifier:@"ViewRoutesCell"];
if (aCell == nil)
{
NSArray *arr = [[NSBundle mainBundle] loadNibNamed:@"ViewRoutesCell" owner:self options:nil];
for (NSObject *anObj in arr) {
AssessObject *newObj1 = [[AssessObject alloc] init];
newObj1=[totalArray objectAtIndex:indexPath.row];
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:newObj1.routeImage]]];
aCell = (ViewRoutesCell *)anObj;
aCell.imgRoute.image = image;
aCell.lblRouteText.text = newObj1.routeText;
aCell.lblRouteImage.text = newObj1.routeImage;
}
}
return aCell;
}
It’s loading to a custom cell. It seems to me that the problem may be related to the reuse of the cells. Any ideas?
You must re-write your code as following:
otherwise, you cells will be cached with old values.