搜索IOS后,项目不会显示在表格视图中

时间:2014-10-24 08:53:27

标签: ios uitableview storyboard uisearchbar

所以我的问题是当我在搜索栏上输入文字时,我无法在tableview中显示项目。

我非常确定除了输入搜索栏时显示项目外,一切正常。 当我使用NSLog时,我可以看到: - filterClients具有从客户端Array中提取的项目 - 我可以看到cellForRowAtIndexPath上的单元格不为空。

我不知道为什么它没有显示在tableview中。

#import <UIKit/UIKit.h>

@interface CRMClientTableVC : UITableViewController  <UISearchBarDelegate, UISearchDisplayDelegate>

@property (strong, nonatomic) NSMutableArray *filterdClients;

@end


#import "CRMClientTableVC.h"
#import "AppDelegate.h"
#import "CRMClientTableViewCell.h"
#import "CRMClientProfileViewController.h"

@interface CRMClientTableVC (){
    NSArray *clients;
}


@end

@implementation CRMClientTableVC{
    id <CRMDataManager> _dataManagerInterface;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    _dataManagerInterface = appDelegate.dataManager;

    clients = [_dataManagerInterface retrieveClients];
    self.filterdClients = [[NSMutableArray alloc] initWithCapacity:clients.count];}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    if(tableView == self.searchDisplayController.searchResultsTableView){
        return [self.filterdClients count];
    }else{
        return [clients count];
    }
}


//SOMETHING DOES NOT WORKKK HERE
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Calling table view");
    CRMClientTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ClientTableViewCell"];
    if (!cell) {
        cell = [[CRMClientTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ClientTableViewCell"];
    }

    if(tableView == self.searchDisplayController.searchResultsTableView){
        CRMClient *client = [self.filterdClients objectAtIndex:indexPath.row];
        [cell populateData:client];
        NSLog(@"Yeahhhhhh");
        NSLog(@"%@", self.filterdClients);
        NSLog(@"%@", cell);
    }else{

        CRMClient *client = [clients objectAtIndex:indexPath.row];
        [cell populateData:client];
         NSLog(@"nahhhhhhhh");
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // reset the cell selected
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    // get selected index to get client data
    CRMClient *selectedClient = [clients objectAtIndex:indexPath.row];

    // pass client data to profile view
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    CRMClientProfileViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"CRMClientProfileViewController"];
    viewController.client = selectedClient;

    [[self navigationController] pushViewController:viewController animated:YES];
}


#pragma mark Content Filtering
-(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
    NSLog(@"%@", searchText);
    [self.filterdClients removeAllObjects];
    // Filter the array using NSPredicate
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];
    self.filterdClients = [NSMutableArray arrayWithArray:[clients filteredArrayUsingPredicate:predicate]];
//    NSLog(@"%@", self.filterdClients);
}


#pragma mark - UISearchDisplayController Delegate Methods
-(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;
}

这是当我输入&#34; Bob ta&#34;时输出中出现的内容。这是客户列表中的一个对象项。

2014-10-24 15:41:49.574 Insurance_CRM[7548:159250] Bob tan
2014-10-24 15:41:49.575 Insurance_CRM[7548:159250] Calling table view
2014-10-24 15:41:49.576 Insurance_CRM[7548:159250] Yeahhhhhh
2014-10-24 15:41:49.576 Insurance_CRM[7548:159250] (
    "<CRMClient: 0x7fe8a0f7c8a0>"
)
2014-10-24 15:41:49.576 Insurance_CRM[7548:159250] <CRMClientTableViewCell: 0x7fe8a0d70ec0; baseClass = UITableViewCell; frame = (0 0; 320 44); hidden = YES; autoresize = W; layer = <CALayer: 0x7fe8a0d70dd0>>

1 个答案:

答案 0 :(得分:0)

您必须重新加载tableView。

使用[self reloadData];

在其中一个搜索委托方法中添加此行。