我知道這是一個常見問題,但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];
}
注:我可以這裏將一些數據從視圖控制器傳遞到另一個沒有問題。
確保您'的tableView:numberOfRowsInSection'沒有返回0 – teamnorge
'numberOfSectionsInTableView'和'numberOfRowsInSection'甚至不叫 – romain64
那麼你的視圖控制器是不聽的UITableViewDelegate,我不知道你是如何在故事板連接它們。但是,如果你100%確定你做的一切正確,如果它適合你的項目,你可以實現'awakeFromNib'方法,你把'actionTableView.delegate = self' – teamnorge