I’m a bit confused on how I can reload the cells in a UITableView after it’s data has been changed. In particular, the confusion I’m having is that the new data could have more or less sections than what is currently on display. I know there’s the reloadSections:withRowAnimation: method, but that requires a 1:1 replacement, where I may or may not have that.
I just want to tell the UITableView to scrap everything and reload as if for the first time. I’d appreciate someone shedding some light on this.
Thanks in advance!
UPDATE
Here’s the code I’m using which I’ve found the issue to be the cells not dequeueing after reloadData is called…
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
NSLog(@"%@", cell);
if (cell == nil) {
NSLog(@"%d; %d", indexPath.section, vehicle.inventoryCategoriesCount);
if (indexPath.section < vehicle.inventoryCategoriesCount) {
NSLog(@"Grouped cell");
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"] autorelease];
NSString *model = [[[vehicle.inventory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"category == %@", [vehicle.inventoryCategories objectAtIndex:indexPath.section]]] valueForKeyPath:@"@distinctUnionOfObjects.model"] objectAtIndex:indexPath.row];
cell.textLabel.text = model;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", [[vehicle.inventory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"model == %@", model]] count]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
} else {
NSLog(@"Remove cell");
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease];
cell.textLabel.text = @"Remove an Item";
cell.textLabel.textColor = [UIColor redColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
NSLog(@"Adding a cell");
return cell;
}
You can call
reloadDataon the table view.The documentation is here:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html