ios - cellForRowAtIndexPath : not called by reloaddata

标签 ios objective-c tableview reloaddata searchbar

我知道这是一个常见问题,但是当我使用 [actionTableView reloadData]; 时,cellForRowAtIndexPath: 没有被调用。由于 Storyboard ,我已经链接了 dataSourcedelegate,但这并没有解决问题。

这是我的部分代码:

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 来过滤数组,但是没有这个它也不起作用。

编辑:问题说明 reloadData 仅当我在 viewController 中调用它时才有效。如果我从另一个 viewController 调用它则不会。显然,对于这两种情况,我调用了位于 tableView 所在的 View Controller 中的相同函数 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];

注意:我可以毫无问题地将一些数据从一个 View Controller 传递到另一个 View Controller 。

最佳答案

在创建 TableView 时,我经常遇到的一件事是使用界面生成器将表与界面连接起来。但是当您在没有与表建立连接的情况下调用 [actionTableView reloadData] 时,什么也不会发生。

我完全忘记了那个 super 简单的步骤。希望这有帮助

关于ios - cellForRowAtIndexPath : not called by reloaddata,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31503758/

相关文章:

ios - 避免隐式展开变量

ios - 在 Xcode Cloud 配置屏幕中更改团队

objective-c - 在 iOS 中读取文件时出现 malloc 错误

JavaFX TableView String[] 列和 String[][] 值

ios - sectionHeader 中的自动调整字体大小

Swift - Tableview 单元格保持突出显示 - 除了最上面的一个 - 无论如何

ios - 停止 Collection View 取消选择一个项目

iphone - iOS 5 自定义标签栏图像垂直对齐

ios - 如何将自定义字体用于uilabel

ios - 带有 Json 的 UITableViewController