I'm trying to filter an NSArray using grand central dispatch. I'm able to filter the array and when I call [tableView reloadData]
the correct values are being printed by NSLog; however the view shows the previous values.
For example, if my collection of items is Red, Orange, Yellow
and I filter for r
, the NSLogs will print that there are 2 rows and the cells are Red
and Orange
, but all three cells will be shown. When the search becomes ra
, NSLog shows there is only 1 row called Orange
, but the cells Red
and Orange
are shown;
- (void)filterItems:(NSString *)pattern{
__weak MYSearchViewController *weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSMutableArray *items = [weakSelf.items copy];
//lots of code to filter the items
dispatch_async(dispatch_get_main_queue(), ^{
weakSelf.items = [items copy];
[weakSelf.tableView reloadData];
});
});
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"Rows: %d",[self.items count]);
return [self.items count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MYCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
NSInteger row = [indexPath row];
MYItem *item = [self.items objectAtIndex:row];
//code to setup cell
NSLog(@"Row %d, Item %@, Cell %d", row, item.info, cell.tag);
return cell;
}