Abhinandan Pratap
Abhinandan Pratap

Reputation: 2148

UISearchBar - results of search not displayed

In my project I'm using custom UISearchBar. I completed my code but when I trying to search anything it doesn't display results. I'm sharing some code snippet.

.H file

#import <UIKit/UIKit.h>

@interface FriendsViewController : UIViewController<UISearchDisplayDelegate , UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>
{
    UITableView *friendslisttable;
    NSMutableArray *friendslist;
    NSMutableArray *friendsnamearray;
    NSMutableArray *profilepicarray;
    UIImageView * frndprofpic;
    UILabel *friendnamelabel;

    NSArray *searchResults;
}
@end

my .m file

#import "FriendsViewController.h"
#import "ProfileViewController.h"

@interface FriendsViewController ()

@end

@implementation FriendsViewController

- (id)init
{
    if (self = [super init])
    {
        self.title = @"Friends Request";
        self.view.backgroundColor = [UIColor colorWithRed:100.0/255.0 green:125.0/255.0 blue:130.0/255.0 alpha:1];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Friends Request";

    UISearchBar *searchh = [[UISearchBar alloc] init];
    searchh.frame = CGRectMake(0 ,65, self.view.frame.size.width,45);
    searchh.delegate = self;
    searchh.showsBookmarkButton = NO;
    searchh.placeholder = @"Search friend";
    searchh.barTintColor = [UIColor whiteColor];
    [self.view addSubview:searchh];        
    friendslisttable = [[UITableView alloc]initWithFrame:CGRectMake(0,110 , self.view.frame.size.width, self.view.frame.size.height)] ;
    friendslisttable.delegate = self;
    friendslisttable. dataSource = self;
    [self.view addSubview:friendslisttable];

    friendsnamearray = [[NSMutableArray alloc]initWithObjects:@"friend1",@"friend12",@"friend13",@"friend14",@"friend15",@"friend16",@"friend17",@"friend18",@"friend19",@"friend20",@"friend21 ",@"friend22",@"", nil];

    profilepicarray = [[NSMutableArray alloc]initWithObjects:@"download3.jpeg", @"12045540_717548405011935_7183980263974928829_o.jpg" , @"download4.jpeg", @"download5.jpeg", @"download6.jpg", @"download12.jpeg", @"download13.jpeg", @"download16.jpeg",@"download3.jpeg", @"download6.jpg", @"download12.jpeg", @"download16.jpeg", @"  ",  nil];
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
    searchResults = [friendsnamearray filteredArrayUsingPredicate:resultPredicate];
}    

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];
    return YES;
}

#pragma mark - TableView Delegate Method -------------------->>>>

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}    

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return searchResults.count;
    }
    else {
        return friendsnamearray.count;
    }
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellidentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier ];

    if(!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellidentifier];
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    }
    friendslisttable  = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        friendslisttable = [searchResults objectAtIndex:indexPath.row];
    } else
    {
        friendslisttable = [friendsnamearray objectAtIndex:indexPath.row];
    }

    frndprofpic = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 50, 50)];

    frndprofpic.image=[UIImage imageNamed:[profilepicarray objectAtIndex:indexPath.row]];
    [cell.contentView addSubview:frndprofpic];

    friendnamelabel = [[UILabel alloc]initWithFrame:CGRectMake(70, 10, 250, 50)];
    friendnamelabel.text = [friendsnamearray objectAtIndex:indexPath.row];
    friendnamelabel.font = [UIFont fontWithName:@"ChalkboardSE-Regular" size:20];
    [cell.contentView addSubview:friendnamelabel];

    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!indexPath.row) {
        ProfileViewController *u = [[ProfileViewController alloc]init];
        [self.navigationController pushViewController:u animated:YES];
    }
}

- (void)tableView:(UITableView *)tableView1 willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    [cell setBackgroundColor:[UIColor clearColor]];
    tableView1.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"Cream.jpg"]];
}

@end

Upvotes: 0

Views: 771

Answers (3)

hbk
hbk

Reputation: 11184

What u need:

  1. Create Search bar like u already do it in viewDidLoad and add delegate for searchBar as self (also done)

  2. use <UISearchBarDelegate>

  3. implement few methods from delegate

like

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText

In this method u should filter u'r dataSource and reload data in tableView.

If all done correctly - u will get expected behavior

Regarding your code:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString

will be never called - try to put breakPoint inside - this is one of the reason

another problem - is your predicate @"name contains %@" - this mean that u have object in array with property name. Actualy u havent any custom objects - only strings, so @"SELF contains[c] %@" should be instead

if u update it u will get correct search result:

enter image description here

next problem - tableViewCell configurations if (tableView == self.searchDisplayController.searchResultsTableView) - will be never true - if u want use it - you should change base class to UISearchDisplayController

So if u correct this - u will get workable solution for search.

Also I recommend u to read first Robert Martin "Clean Code" - ISBN-13: 978-0132350884


Also this post or this one can be helpful

Upvotes: 1

Adeel Miraj
Adeel Miraj

Reputation: 2496

In tableView:cellForRowAtIndexPath: why are you doing this -> friendslisttable = nil? The other thing that you are doing wrong is that you assign an NSString type value to your friendslisttable which is actually a UITableView instance. That is why you are getting this error 'NSInvalidArgumentException', reason: '-[__NSCFConstantString reloadData]: unrecognized selector sent to instance.

I suggest that you should follow this tutorial to get aligned with the concept of search controller. http://useyourloaf.com/blog/updating-to-the-ios-8-search-controller.html

Upvotes: 1

Ashish Thakkar
Ashish Thakkar

Reputation: 944

Might be you forget to reload your tableview

[friendslisttable reloadData];

Upvotes: 1

Related Questions