cellForRowAtIndexPath:reloaddata 未调用
cellForRowAtIndexPath: not called by reloaddata
我知道这是一个常见问题,但是当我使用 [actionTableView reloadData];
时 cellForRowAtIndexPath: 没有被调用。多亏了故事板,我已经链接了 dataSource 和 delegate,但它并没有解决问题。
这是我的部分代码:
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;
}
如您所见,我也使用 searchBar 来过滤数组,但是 w/o 这也不起作用。
编辑:问题说明: reloadData
仅当我在 viewController 中调用它时才有效。如果我从另一个 viewController 调用它就不会。显然,对于这两种情况,我调用了位于 tableView 所在的 viewcontroller 中的相同函数 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];
}
注意:我可以毫无问题地将一些数据从一个视图控制器传递到另一个视图控制器。
我总是 运行 在努力建立 table 视图时,我总是 table 的一件事是使用界面生成器将 table 与界面连接起来。但是当你在没有连接到 table 的情况下调用 [actionTableView reloadData] 时,什么也不会发生。
我完全忘记了那个超级简单的步骤。希望这对您有所帮助
问题是您从未将 actionsArray
和 filteredActionArray
初始化为非零。因此,numberOfRowsInSection 始终 returns 0,因此不会调用 cellForRowAtIndexPath。
终于找到解决办法了!
在tableView所在的SearchViewController.m中,把这个放在viewDidLoad
中
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatingTableView) name:@"reload_data" object:nil];
还有:
-(void)updatingTableView{
[actionTableView reloadData];
}
在AnotherViewController.m
[[NSNotificationCenter defaultCenter] postNotificationName:@"reload_data" object:self];
how to reload tableview of another uiviewcontroller in current viewcontroller
谢谢大家!
我知道这是一个常见问题,但是当我使用 [actionTableView reloadData];
时 cellForRowAtIndexPath: 没有被调用。多亏了故事板,我已经链接了 dataSource 和 delegate,但它并没有解决问题。
这是我的部分代码:
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;
}
如您所见,我也使用 searchBar 来过滤数组,但是 w/o 这也不起作用。
编辑:问题说明: reloadData
仅当我在 viewController 中调用它时才有效。如果我从另一个 viewController 调用它就不会。显然,对于这两种情况,我调用了位于 tableView 所在的 viewcontroller 中的相同函数 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];
}
注意:我可以毫无问题地将一些数据从一个视图控制器传递到另一个视图控制器。
我总是 运行 在努力建立 table 视图时,我总是 table 的一件事是使用界面生成器将 table 与界面连接起来。但是当你在没有连接到 table 的情况下调用 [actionTableView reloadData] 时,什么也不会发生。
我完全忘记了那个超级简单的步骤。希望这对您有所帮助
问题是您从未将 actionsArray
和 filteredActionArray
初始化为非零。因此,numberOfRowsInSection 始终 returns 0,因此不会调用 cellForRowAtIndexPath。
终于找到解决办法了!
在tableView所在的SearchViewController.m中,把这个放在viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatingTableView) name:@"reload_data" object:nil];
还有:
-(void)updatingTableView{
[actionTableView reloadData];
}
在AnotherViewController.m
[[NSNotificationCenter defaultCenter] postNotificationName:@"reload_data" object:self];
how to reload tableview of another uiviewcontroller in current viewcontroller
谢谢大家!