I'm an Android developer working on my first iOS project. I have a UITableView that displays almost 37,500 rows. One row for each item in an grocery store. The list has 3 columns one containing the item name and the other 2 containing other important data. The columns are sortable, and to handle the sorting I sort the data array as I need to and call [tableView reloadData]
after I'm done sorting the array. This works fine except there is a long delay of at least a few seconds after reloading the data where the main thread is locked up doing work. I'm no stranger to list performance as I've had to make smooth lists numerous times in Android. So from what I can tell I'm not really doing much to cause this. The only thing I can think of is just the large number of items in my array. Here is the relevant code:
Here are the table methods I am overriding:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.data count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"CustomCell";
ReplenishListCell *cell = [self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[ReplenishListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSMutableDictionary *dictData = [self.data objectAtIndex:indexPath.row];
cell.nameLabel.text = dictData[@"item-description"];
cell.firstLicationColumnLabel.text = dictData[@"store-count"];
cell.secondLicationColumnLabel.text = dictData[@"other-count"];
return cell;
}
-(void)tableView:(UITableView *)replenishTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self showActivityIndicator];
ReplenishListCell *cell = (ReplenishListCell*) [replenishTableView cellForRowAtIndexPath:indexPath];
NSString *nameClicked = cell.nameLabel.text;
[database getItemByName:nameClicked :self];
}
Here is the method I use to sort the array:
-(void) sortArray:(NSString *) dictionaryKey {
NSSortDescriptor *sortByName = [NSSortDescriptor sortDescriptorWithKey:dictionaryKey ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortByName];
NSArray *sortedArray = [data sortedArrayUsingDescriptors:sortDescriptors];
[data removeAllObjects];
[data addObjectsFromArray:sortedArray];
[self.tableView reloadData];
}
I do not have any performance issues until after calling [self.tableView reloadData]
. So I'm wondering if there is something I'm missing, or is there maybe a better way of reloading the data besides reloadData
? Any help will be greatly appreciated. I've spent a few hours now debugging and Googling and I haven't come up with a solution yet.