I have a UITableViewController which uses a NSMutableArray for it's datasource. The array is initially populated in the viewDidLoad method by calling a web service to retrieve the data and populate the NSMutableArray. The user then has the option to enter a search term (in a UISearchBar) and press Search to rerun the web service and repopulate the table list. At this point I need to empty the array and repopulate the table from the results of the web service. My question is what is the correct way to do this without upsetting the repainting of the table view? I can see 2 options :
Call [listArray removeAllObjects]; then [[self tableView] reloadData]; before repopulating the array.
As I restrict the list to a maximum of 200 rows, initialise the array with 200 items and then instead of removing and re-adding when the search is run, use [listArray replaceObjectAtIndex: to replace each row. This would then require an int variable to keep the number of rows returned and use this in the 'tableView numberOfRowsInSection' method so the tableview only displays the number of rows returned.
Hope that makes sense! I'm asking the question because I've had some intermittent errors (EXC_BAD_ACCESS) when re-loading the list and I'm convinced it's to do with how I'm emptying and reloading the list so could use some advice on the best approach.
Any help appreciated,
Jonathan
UPDATE :
Code to initialise array in viewDidLoad :
tableListDataArray = [[NSMutableArray alloc] init];
Once data is retrieved, it is added to the array as follows :
CustSuppListItem *custSuppItem = [[CustSuppListItem alloc] init];
[custSuppItem setAcCode:[jsonCustSuppRecord getStringForKey:@"acCode"]];
[custSuppItem setAcCompany:[jsonCustSuppRecord getStringForKey:@"acCompany"]];
[custSuppItem setAcContact:[jsonCustSuppRecord getStringForKey:@"acContact"]];
[custSuppItem setOsBalBase:[jsonCustSuppRecord getDoubleForKey:@"osBalBase"]];
[custSuppItem setAcAccStatus:[jsonCustSuppRecord getIntForKey:@"acAccStatus"]];
[tableListDataArray addObject:custSuppItem];
[custSuppItem release];
The array is released in the dealloc method as follows :
[tableListDataArray release];
I personally would go with option 1. You should just be able to use
without the "self". reloadData will also call the cellForRowAtIndexPath method I believe Don't forget to initialize your NSMutableArray before using it.