Search code examples
iosobjective-ctableviewreloaddatasearchbar

cellForRowAtIndexPath: not called by reloaddata


I know it's a commom problem but the cellForRowAtIndexPath: is not called when I use [actionTableView reloadData];. I already linked dataSource and delegate thanks to the storyboard but it doesn't solve the issue.

Here's a part of my code:

SearchViewController.h

#import <UIKit/UIKit.h>

@interface SearchViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchDisplayDelegate>

@property (strong, nonatomic) IBOutlet UITableView *actionTableView;
@property (strong, nonatomic) IBOutlet UISearchBar *actionSearchBar;
@property (strong, nonatomic) NSMutableArray *actionsArray;
@property (strong, nonatomic) NSMutableArray *filteredActionArray;

@end

SearchViewController.m

#import "SearchViewController.h"
#import "Action.h"

@interface SearchViewController ()

@end

@implementation SearchViewController

@synthesize actionsArray, actionTableView, filteredActionArray, actionSearchBar;

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [filteredActionArray count];
    } else {
        return [actionsArray count];
    }
}

-(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];
    }

    NSLog(@"Aloha");

    // Create a new Action object
    Action *a = nil;

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        a = [filteredActionArray objectAtIndex:indexPath.row];
    } else {
        a = [actionsArray objectAtIndex:indexPath.row];
    }

    // Configure the cell
    cell.textLabel.text = a.nomAction;
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    return cell;
}

-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
    // Update the filtered array based on the search text and scope.
    // Remove all objects from the filtered search array
    [self.filteredActionArray removeAllObjects];
    // Filter the array using NSPredicate
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.nomAction contains[c] %@",searchText];
    filteredActionArray = [NSMutableArray arrayWithArray:[actionsArray filteredArrayUsingPredicate:predicate]];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    // Tells the table data source to reload when text changes
    [self filterContentForSearchText:searchString scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles]objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
    // Tells the table data source to reload when scope bar selection changes
    [self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

As you see I also use a searchBar to filter the array, but w/o this it doesn't work either.

EDIT: SPECIFICATION OF THE PROBLEM: reloadData works only if I call it within the viewController. Not if I call it from another viewController. Obviously for the two cases I call the same function updatingTableView located in the viewcontroller where the tableView is. Any ideas to reload the tableView from anotherViewController?

SearchViewController.m (works)

-(IBAction)update{
    [self updatingTableView:nil];
}

-(void)updatingTableView:(NSData*)someData {
   actionsArray = [NSArray arrayWithObjects:@"item1", @"item2", @"item3", nil];
   [actionTableView reloadData];
}

AnotherViewController.m (does not work)

-(IBAction)updateFromElsewhere{
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main"
                                                         bundle: nil];

    SearchViewController *searchViewController = (SearchViewController*)    [mainStoryboard
                                                             instantiateViewControllerWithIdentifier: @"searchcontroller_id"];

    [searchViewController updatingTableView:nil];

}

NB: I can pass some data from a view controller to the other without problem here.


Solution

  • Finally found the solution!

    In SearchViewController.m where the tableView is, put this in viewDidLoad

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatingTableView) name:@"reload_data" object:nil];
    

    With still:

    -(void)updatingTableView{
        [actionTableView reloadData];
    }
    

    In AnotherViewController.m

    [[NSNotificationCenter defaultCenter] postNotificationName:@"reload_data" object:self];
    

    how to reload tableview of another uiviewcontroller in current viewcontroller

    Thanks everyone!