2015-07-19 67 views
0

我知道這是一個常見問題,但cellForRowAtIndexPath:在我使用[actionTableView reloadData];時未被調用。我已經鏈接dataSource代表感謝故事板,但它不能解決問題。cellForRowAtIndexPath:not reloaddata

這裏是我的代碼的一部分:

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

正如你看到的我也使用搜索欄來過濾數組,但沒有這個也不起作用。

編輯:問題規範:reloadData只有在我在viewController中調用它時才起作用。不,如果我從另一個viewController調用它。很明顯,在這兩種情況下,我調用位於tableView所在視圖控制器中的相同函數updatingTableView。任何想法從另一個ViewController重新加載tableView?

SearchViewController.m工作

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

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

AnotherViewController.m不起作用

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

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

    [searchViewController updatingTableView:nil]; 

}

注:我可以這裏將一些數據從視圖控制器傳遞到另一個沒有問題。

+1

確保您'的tableView:numberOfRowsInSection'沒有返回0 – teamnorge

+0

'numberOfSectionsInTableView'和'numberOfRowsInSection'甚至不叫 – romain64

+0

那麼你的視圖控制器是不聽的UITableViewDelegate,我不知道你是如何在故事板連接它們。但是,如果你100%確定你做的一切正確,如果它適合你的項目,你可以實現'awakeFromNib'方法,你把'actionTableView.delegate = self' – teamnorge

回答

0

終於找到了解決辦法!

SearchViewController.m其中的tableView是,把這個viewDidLoad

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

仍處於:

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

AnotherViewController。米

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

how to reload tableview of another uiviewcontroller in current viewcontroller

謝謝大家!

+0

因此,您必須刪除通知? – ErickES7

1

當掙扎表視圖時,我總是遇到的一件事是使用界面構建器將表與接口連接起來。但是當你調用[actionTableView reloadData]而沒有連接到表時,什麼都不會發生。

我的方式忘記了超級簡單的一步。希望這有幫助

+0

我至少驗證了十次! 'actionTableView'出口,'委託'和'dataSource'全都連接到IB – romain64

+0

當!好吧,你知道這些方法並沒有被稱爲BC你有一個斷點?你怎麼知道他們不被稱爲? – ErickES7

+0

斷點和NSLog :) – romain64

0

問題是你永遠不會初始化actionsArrayfilteredActionArray是非零。因此,numberOfRowsInSection總是返回0,因此不會調用cellForRowAtIndexPath。

+0

在調用'reloadData'之前,actionsArray會加載另一個函數,這裏沒有顯示。在重新加載數據之前,我檢查了NSArray的內容,並且一切正常。 – romain64

+0

這是超級奇怪的,所以numberOfSectionsInTableView:永遠不會被調用?你已經在函數頭**上放置了一個斷點**而沒有任何東西? Bc任何將其數據源連接到任何表的視圖將在加載視圖時自動加載其數據,它應該至少調用這些函數一次 – ErickES7