1
votes

So My problem is i can't get items to show in tableview when i type text on the search bar.

I'm pretty sure that everything is working except for the showing items when typing search bar. When i using NSLog i can see that: - the filterClients having items extracts from the clients Array - i can see that the cell at cellForRowAtIndexPath is not null.

I don't know why it does not show in 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;
}

This is what appear in the output when i type "Bob ta" which is one object item of the clients list.

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

1 Answers

0
votes

You must reload the tableView.

Use [self reloadData];

Add this line in one of the search delegate methods.