I have UIView designed in IB, and UITableView on it. In view contoller’s viewDidLoad method I init my custom UITableViewContoller and sets dataSource and delegate:
- (void)viewDidLoad {
[super viewDidLoad];
...
GoodsTableController *goodsController = [[GoodsTableController alloc] init];
goodsController.data = [[orderData objectForKey:@"Result"] objectForKey:@"Items"];
self.gtc = goodsController;
[goodsController release];
goodsTable.delegate = self.gtc;
goodsTable.dataSource = self.gtc;
[goodsTable reloadData];}
Intresting in GoodsTableController:
//in the header file
@interface GoodsTableController : UITableViewController {
NSMutableArray *data;
}
@property (nonatomic, retain) NSMutableArray *data;
//implementation
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"Current data size %d", [data count]);
return [data count];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(@"Deleting row at index %d", indexPath.row);
[self.tableView beginUpdates];
[self.data removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
[self.tableView reloadData];
}
}
For debug I used NSLog, and I saw that array was changed, its size was changed, tableView:numberOfRowsInSection was called, but table don’t update. Row don’t hides. That is log:
2011-04-07 13:08:18.784 Test[32300:207] Current data size 2 //first call tableView:numberOfRowsInSection: by reloadData in View Controller after set data
2011-04-07 13:08:23.486 Test[32300:207] Current data size 2 //call after slide on row
2011-04-07 13:08:26.091 Test[32300:207] Deleting row at index 0
2011-04-07 13:08:26.093 Test[32300:207] Current data size 1 //call after endUpdate
2011-04-07 13:08:26.096 Test[32300:207] Current data size 1 //call after reloadData
The problem was in undefined tableView in GoodsTableController instance.
Fixed code: