raaj
raaj

Reputation: 3311

reloadData not working (called in delegate)

Good Day,

I am using UITableViewController to display a Search Items.

My code is as follows below: The problem is, when I call my GETSEARCH function in my viewDidLoad, it runs and performs the callback TITLEITEMSRETURNED. And the tableView reloads correctly.

However, if I use the searchbar and perform the GETSEARCH. the delegate gets called, the data loaded into the array correctly, but the tableView never gets updated.

However, if I press the grey cross button, the table suddenly updates!!? What gives?

-(void)TitleItemsReturned:(NSArray*)titleItems{
    for(TitleItem* titleItem in titleItems){
        // NSLog(@"TITLE: %@ ISBN: %@",titleItem.Title,titleItem.ISBN);
        [searchResults addObject:titleItem];
    }
    [self.tableView reloadData];
}

- (void)viewDidLoad
{
    NSLog(@"RUN");
    networkLayer=[[NLBNetworkLayer alloc]init];
    searchResults=[[NSMutableArray alloc]initWithCapacity:500];
//  [networkLayer getBookSearch:TITLE term:@"Inferno"];
    [super viewDidLoad];
}

-(void)viewDidAppear:(BOOL)animated{
    [networkLayer setDelegate:(id)self];
}


#pragma mark - Table view data source

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if ( cell == nil ) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    TitleItem *titleItem = nil;
    titleItem = [searchResults objectAtIndex:indexPath.row];
// Configure the cell
    cell.textLabel.text = titleItem.Title;
    NSLog(@"called %@",titleItem.Title);
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"count %d",[searchResults count]);
    return [searchResults count];
}

#pragma mark - UISearchDisplayController Delegate Methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller     shouldReloadTableForSearchString:(NSString *)searchString {
    return YES;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    //[networkLayer getBookSearch:TITLE term:searchBar.text];
    [networkLayer getBookSearch:TITLE term:@"Inferno"];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    NSLog(@"all removed");
    [searchResults removeAllObjects];
    [self.tableView reloadData];
}

Upvotes: 2

Views: 1325

Answers (1)

Mike Mertsock
Mike Mertsock

Reputation: 12025

Make sure you are sending the reloadData message from the main thread, otherwise you can have problems. It appears that the TitleItemsReturned method may not be called from the main thread (e.g. from a background thread in a NSURLConnectionDelegate method implemented by the networkLayer object, or a similar delegate method).

If TitleItemsReturned is indeed not being run on the main thread, you can do this inside of TitleItemsReturned:

dispatch_async(dispatch_get_main_queue(), ^{
    [self.tableView reloadData];
});

The searchBarCancelButtonClicked method is working because that method is running on the main thread (from a UI event).

Upvotes: 5

Related Questions