简体   繁体   中英

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.

One thing I always run into when struggle up a table view is connecting the table with the interface using interface builder. But when you call [actionTableView reloadData] without having the connection made to the table, nothing will happen.

I ways forget that super simple step. Hope this helped

The problem is you never initialize actionsArray and filteredActionArray to be non-nil. As a result, numberOfRowsInSection always returns 0, so cellForRowAtIndexPath won't be called.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM