I have an app which displays quite a lot of data in a UITableView. I already added the UISearchBar and UISearchDisplayController in Interface Builder to the UITableView. But I do not know how to use it. If someone could provide a quick solution to this, I would be grateful. I just require it to work as you type to find matches of the search query in the UITableView cells (or from an array).
UPDATE 1: Here's the code from thenumberOfRowsInSection method:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (isSearching) {
return [searchResults count];
}
else {
if (section == 0) {
return 1;
}
else if (section == 1) {
return [basicQuantities count];
}
else if (section == 2) {
return [physicalQuantities count];
}
}
return nil;
}
First add the UISearchDisplayController to your table view
Then set its delegate.
Implement the following methods.
Demo Project
In your .h File
#interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *contentList;
NSMutableArray *filteredContentList;
BOOL isSearching;
}
#property (strong, nonatomic) IBOutlet UITableView *tblContentList;
#property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
#property (strong, nonatomic) IBOutlet UISearchDisplayController *searchBarController;
In your .m File
Filling the sample data (Optional Only For Demo Purpose)
- (void)viewDidLoad {
[super viewDidLoad];
contentList = [[NSMutableArray alloc] initWithObjects:#"iPhone", #"iPod", #"iPod touch", #"iMac", #"Mac Pro", #"iBook",#"MacBook", #"MacBook Pro", #"PowerBook", nil];
filteredContentList = [[NSMutableArray alloc] init];
}
Now implement the Table View Delegate and Datasource
- (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 (isSearching) {
return [filteredContentList count];
}
else {
return [contentList 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];
}
// Configure the cell...
if (isSearching) {
cell.textLabel.text = [filteredContentList objectAtIndex:indexPath.row];
}
else {
cell.textLabel.text = [contentList objectAtIndex:indexPath.row];
}
return cell;
}
Search Function Responsible For Searching
- (void)searchTableList {
NSString *searchString = searchBar.text;
for (NSString *tempStr in contentList) {
NSComparisonResult result = [tempStr compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchString length])];
if (result == NSOrderedSame) {
[filteredContentList addObject:tempStr];
}
}
}
Search Bar Implementation
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
isSearching = YES;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSLog(#"Text change - %d",isSearching);
//Remove all objects first.
[filteredContentList removeAllObjects];
if([searchText length] != 0) {
isSearching = YES;
[self searchTableList];
}
else {
isSearching = NO;
}
// [self.tblContentList reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
NSLog(#"Cancel clicked");
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSLog(#"Search Clicked");
[self searchTableList];
}
Here is a good tutorial how to do that. It's too much to just write about it :)
http://www.appcoda.com/how-to-add-search-bar-uitableview/
Related
I have created an app where I am using google firebase for backend and Objective-C for front-end.
It successfully storing and displaying data on UITableView.
Now, I am trying to add Search Bar above the table view and tried many examples from the internet but most of them are outdated.
Can anyone please help me with, how I can add search bar above the table view in Objective-C for iOS 9 or later?
Also, How to filter data when we type in the search bar?
Thanks Heaps
Declare in .h file:
BOOL isFiltered;
NSMutableArray *arrsearchresult ,*arrsearchname;
NSMutableArray *arrfullname;
#property (strong, nonatomic) IBOutlet UITableView *tblAppoinment;
Declare in .m file:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
/*if (tableView == self.searchDisplayController.searchResultsTableView) {
return [arrsearchresult count];
} else {
return [arrfullname count];
}*/
if(isFiltered)
{
return [arrsearchresult count];
}
return [arrfullname count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
AppoinmentTableViewCell *cell = (AppoinmentTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"AppoinmentTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
if(isFiltered)
{
cell.lblFullname.text = [arrsearchresult objectAtIndex:indexPath.row];
}
}
if(isFiltered)
{
cell.lblFullname.text = [arrsearchname objectAtIndex:indexPath.row];
}
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
isFiltered = YES;
}
- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if(searchText.length==0)
{
isFiltered=NO;
}
else
{
isFiltered=YES;
arrsearchresult=[[NSMutableArray alloc]init];
arrsearchdate=[[NSMutableArray alloc]init];
arrsearchname=[[NSMutableArray alloc]init];
for ( NSString * str in arrfullname)
{
NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];
NSInteger index;
if(stringRange.location != NSNotFound)
{
[arrsearchresult addObject:str];
index = [arrfullname indexOfObject:str];
[arrsearchdate addObject:[arrleadCreatedDate objectAtIndex:index]];
[arrsearchname addObject:str];
}
}
}
[self.tblAppoinment reloadData];
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[self.tblAppoinment resignFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
isFiltered=NO;
[self.tblAppoinment reloadData];
}
Though you are talking about search bar. but according to your requirement, it's better to use Search Controller than search bar standalone. Because there are many delegates method you will find along with these.So you don't have to take headache for extra effort.
This code is for filter array.
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:
(NSRange)range replacementText:(NSString *)text
{
NSArray *resultarray;
NSString *strSearch = [NSString stringWithFormat:#"%#",searchB.text];
if ([strSearch isEqualToString:#""])
{
[self showAllData];
return;
}
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"self CONTAINS[cd] %#",strSearch];
resultarray = [self.arrayALLData filteredArrayUsingPredicate:predicate];
[self.arrayPeopleList removeAllObjects];
[self.arrayPeopleList addObjectsFromArray:resultarray];
[_tblView reloadData];
return YES;
}
Here I have a Search Bar on the top of the UIViewController, and also a UITableView below the Search Bar.
When I don't Search things, the tableView could display things well, but when I search, the searchResultTableView can only show some white cell, not the correct result.
Here is the .h file:
#import <UIKit/UIKit.h>
#interface MySearchViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *myTableView;
- (void)reloadView;
#end
And here is the .m file:
#import "MySearchViewController.h"
#import "MyEventInfo.h"
#import "MyEventTableViewCell.h"
#import "MyEventDetailViewController.h"
#interface MySearchViewController ()
#property NSUserDefaults *usrDefault;
#end
#implementation MySearchViewController {
NSMutableArray *events;
NSArray *searchResults;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.usrDefault = [NSUserDefaults standardUserDefaults];
events = [[NSMutableArray alloc] init];
[self extractEventArrayData];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
[self reloadView];
}
- (void)reloadView {
NSLog(#"reloadView");
events = [[NSMutableArray alloc] init];
[self extractEventArrayData];
[self.myTableView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)extractEventArrayData {
NSArray *dataArray = [[NSArray alloc] initWithArray:[self.usrDefault objectForKey:#"eventDataArray"]];
for (NSData *dataObject in dataArray) {
MyEventInfo *eventDecodedObject = [NSKeyedUnarchiver unarchiveObjectWithData:dataObject];
[events addObject:eventDecodedObject];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.myTableView) {
return [events count];
} else {
NSLog(#"searchResults count:%lu",(unsigned long)[searchResults count]);
return [searchResults count];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 360;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomTableCell";
MyEventTableViewCell *cell = (MyEventTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[MyEventTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Display MyEventTableViewCell in the table cell
MyEventInfo *event = nil;
if (tableView == self.myTableView) {
event = [events objectAtIndex:indexPath.row];
} else {
event = [searchResults objectAtIndex:indexPath.row];
NSLog(#"name of event:%#", event.nameOfEvent);
}
cell.nameOfEvent.text = event.nameOfEvent;
cell.imageOfEvent.image = [UIImage imageNamed:event.imageOfEvent];
cell.timeOfEvent.text = event.timeOfEvent;
cell.locationOfEvent.text = event.locationOfEvent;
cell.dateOfEvent.text = event.dateOfEvent;
return cell;
}
- (void)filterContentForSearchText:(NSString*)searchText
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"nameOfEvent contains[c] %#", searchText];
searchResults = [events filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString];
return YES;
}
#end
And on the storyboard I've already set the tableView's delegate and datasource to self. This drives me crazy, I cannot figure out what's wrong. Could someone help me out? Thanks.
when searching, cell's objects were all null, and had nothing in itself
when not searching, the cell is well with objects that had content the same as event
I'm trying to implement a UISearchBar in a custom UITableViewController and done programmatically (not using IB). I got the search function to work and return the correct fields, but it is displaying the searched cells over the full list cells:
As you can see, the new searched field is scrollable and selectable. Its just not removing the old cells.
here is my .h file:
#interface TestTableViewController : UITableViewController <UISearchBarDelegate, UISearchDisplayDelegate>
#property (strong, nonatomic) NSArray *boundaries;
#end
.m file:
#import "TestTableViewController.h"
#import "Boundary.h"
#interface TestTableViewController ()
#property (strong, nonatomic) UISearchDisplayController *searchController;
#property (strong, nonatomic) NSMutableArray *filteredBoundaries;
#end
#implementation TestTableViewController
-(instancetype) initWithStyle:(UITableViewStyle)style {
self = [super initWithStyle:style];
if (self) {
self.filteredBoundaries = [NSMutableArray array];
}
return self;
}
-(void)viewDidLoad {
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:TRUE selector:#selector(caseInsensitiveCompare:)];
NSArray *sortDescriptors = #[sortDescriptor];
self.boundaries = [self.boundaries sortedArrayUsingDescriptors:sortDescriptors];
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
searchBar.delegate = self;
searchBar.placeholder = #"Search Fields";
searchBar.showsCancelButton = TRUE;
self.tableView.tableHeaderView = searchBar;
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
}
// ------------------------------------------------------------------------------------------------------
#pragma mark -
#pragma mark Setup Filter Data Source
-(void)filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope {
[self.filteredBoundaries removeAllObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"name contains[c] %#", searchText];
self.filteredBoundaries = [NSMutableArray arrayWithArray:[self.boundaries filteredArrayUsingPredicate:predicate]];
}
// ------------------------------------------------------------------------------------------------------
#pragma mark -
#pragma mark Table view data source
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.searchDisplayController.searchResultsTableView) {
return self.filteredBoundaries.count;
}
else {
return self.boundaries.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];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
Boundary *boundary = [self.filteredBoundaries objectAtIndex:indexPath.row];
cell.textLabel.text = boundary.name;
cell.textLabel.textColor = [UIColor blackColor];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
else {
Boundary *boundary = [self.boundaries objectAtIndex:indexPath.row];
cell.textLabel.text = boundary.name;
cell.textLabel.textColor = [UIColor blackColor];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.userInteractionEnabled = TRUE;
}
return cell;
}
// ------------------------------------------------------------------------------------------------------
#pragma mark -
#pragma mark UISearchDisplayController Delegates
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString scope:[[self.searchController.searchBar scopeButtonTitles] objectAtIndex:self.searchController.searchBar.selectedScopeButtonIndex]];
return TRUE;
}
#end
And how I call the table view:
TestTableViewController *tableViewController = [[TestTableViewController alloc] initWithStyle:UITableViewStylePlain];
tableViewController.boundaries = [group.boundaries allObjects];
tableViewController.contentSizeForViewInPopover = POPOVER_SIZE;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:tableViewController];
navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
navController.navigationBar.barStyle = UIBarStyleBlack;
self.myPopoverController = [[UIPopoverController alloc] initWithContentViewController:navController];
self.myPopoverController.delegate = self;
[self.myPopoverController presentPopoverFromRect:button.frame inView:button.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
Any ideas what I might be doing wrong or missing?
The problem is that UISearchDisplayController is using another UITableView rather than the view controller's own. You can verify that by logging tableView in -tableView:cellForRowAtIndexPath:.
You can use a UISearchBar without a UISearchDisplayController, to have more control over search and display logic.
Also, if your app doesn't support any version prior to iOS 8, consider using UISearchController. I haven't tried it but it seems to give you more control. Check a sample UISearchDisplayControllerhas been deprecated in iOS 8.
Try this
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 41.0)];
[self.view addSubview:searchBar];
searchBar.delegate=self;
- (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 (isSearching) {
return [filteredContentList count];
}
else {
return [titlearray count];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (isSearching)
{
cell.nameLabel.text = [filteredContentList objectAtIndex:indexPath.row];
cell.thumbnailImageView.image =[filteredImgArray objectAtIndex:indexPath.row];
}
else
{
cell.thumbnailImageView.image = [imagearray objectAtIndex:indexPath.row];
cell.nameLabel.text = [titlearray objectAtIndex:indexPath.row];
}
return cell;
}
- (void)searchTableList {
NSString *searchString = searchBar.text;
for (int i=0; i<titlearray.count; i++) {
NSString *tempStr=[titlearray objectAtIndex:i];
NSComparisonResult result = [tempStr compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchString length])];
if (result == NSOrderedSame)
{
[filteredContentList addObject:tempStr];
[filteredImgArray addObject:[imagearray objectAtIndex:i]];
}
}
}
#pragma mark - Search Implementation
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
isSearching = YES;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSLog(#"Text change - %d",isSearching);
//Remove all objects first.
[filteredContentList removeAllObjects];
[filteredImgArray removeAllObjects];
if([searchText length] != 0) {
isSearching = YES;
[self searchTableList];
//tblContentList.hidden=NO;
}
else {
isSearching = NO;
// tblContentList.hidden=YES;
}
[tblContentList reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
NSLog(#"Cancel clicked");
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSLog(#"Search Clicked");
[self searchTableList];
}
I hope it's help for you
You should implement correct datasource.
Create new array of items for filtered data for first.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger count = ([filteredItems count] > 0) ? [filteredItems count] : [self.allItems count];
return count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier: #"id"];
MyCustomItem *item = ([filteredItems count] > 0) ? filteredItems[indexPath.row] : self.allItems[indexPath.row];
[self configureCell:cell forItem:item];
return cell;
}
Configure searching:
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSString *searchText = searchController.searchBar.text;
if ([searchText length] == 0)
{
[filteredItems removeAllObjects];
[self.tableView reloadData];
return;
}
NSMutableArray *searchResults = [self.allItems mutableCopy];
// SKIP ALL BODY OF SEARCHING
filteredPeoples = searchResults;
[self.tableView reloadData];
}
Will work pretty.
IOS 8 delegate has been deprecated not sure if that's the problem.
The method
here's [a link]https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UISearchDisplayDelegate_Protocol/index.html#//apple_ref/occ/intfm/UISearchDisplayDelegate/searchDisplayControS 8 delegate has been deprecated not sure if that's the problem.
The method
try this property instead
#property(nonatomic, assign) id< UISearchResultsUpdating > searchResultsUpdater
another better link [a link]https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Listings/TableSearch_obj_c_TableSearch_APLResultsTableController_m.html
I am having issues with my UISearchBar that I have implemented. My app will compile and run, but as soon as I hit the search bar to begin typing a word in, it crashes. Can anyone tell me if you see anything wrong with my syntax? It is suppose to go through the states that I have in my NSArray.
TableViewController.h:
#import <UIKit/UIKit.h>
#interface TableViewController : UITableViewController <UISearchBarDelegate>
#property (nonatomic, strong) NSMutableArray *results;
#property (nonatomic, strong) IBOutlet UISearchBar *searchBar;
#end
TableViewController.m:
#import "TableViewController.h"
#import "ViewController.h"
#interface TableViewController ()
#end
#implementation TableViewController
{
NSArray *states;
}
- (NSMutableArray *)results
{
if (!_results)
{
_results = [[NSMutableArray alloc] init];
}
return _results;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
states = [NSArray arrayWithObjects:#"Alabama", #"Georgia", #"Tennessee", #"Colorado", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)searchThroughData
{
self.results = nil;
NSPredicate *resultsPredicate = [NSPredicate predicateWithFormat:#"SELF contains [search] $#", self.searchBar.text];
self.results = [[states filteredArrayUsingPredicate:resultsPredicate] mutableCopy];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[self searchThroughData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView == self.tableView)
{
return [states count];
}
else
{
[self searchThroughData];
return self.results.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if(tableView == self.tableView)
{
cell.textLabel.text = [states objectAtIndex:indexPath.row];
}
else
{
cell.textLabel.text = self.results[indexPath.row];
}
return cell;
}
I am fairly new to Objective-C, so sorry if this is a simple question that I should know the answer to.
Thanks
The first line of your - (void)searchThroughData method sets the NSMutableArray to nil. This may be the problem.
I am pretty new in objective c so hopefully this all make sense.I have followed this video tutorial from YouTube.The program is not working .
You can download the project from this link.Guide me
to make this program functional.
use the following tutorial is the simple way to get the answer and and also u get the sample code in github, the link is http://www.appcoda.com/how-to-add-search-bar-uitableview/
in your view controller.h file
import
#interface RecipeBookViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
NSArray *recipes; //this is used for load the data to table
NSArray *searchResults; //this is used for search the particular name in uitableview
}
#property (nonatomic, strong) UITableView *tableView; //progrmatically allocationg the tableview
.m file
#synthesize tableView; //this is used accessing the name in more than one time
- (void)viewDidLoad
{
[super viewDidLoad];
// Initialize table data
searchResults=[NSArray alloc]init];
recipes = [NSArray arrayWithObjects:#"Egg Benedict", #"Mushroom Risotto", #"Full Breakfast", #"Hamburger", #"Green Tea", #"Thai Shrimp Cake", #"Angry Birds Cake", #"Ham and Cheese Panini", nil];
//create the tableview
tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 163, 320, 317)];
tableView.delegate=self;
tableView.dataSource=self;
[self. tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"cell"];
[self.view addSubview: tableView];
}
//this is for loading the table data
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [recipes count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
}
return cell;
}
//this is for search bar delegate method for filtering
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:#"SELF contains[cd] %#",
searchText];
searchResults = [recipes filteredArrayUsingPredicate:resultPredicate];
}
#pragma mark - UISearchDisplayController delegate methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex: [self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
finally add the one search bar display controller only in top on the view
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar; // called when text starts editing
{
if (searching)
{
searching=YES;
letsUserSelectRow=YES;
}
else
{
searching=NO;
letsUserSelectRow=YES;
}
// self.tbl_searchtable.scrollEnabled=NO;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
searching = NO;
letsUserSelectRow = YES;
searchBar.text = #"";
[searchBar resignFirstResponder];
[self.tbl_searchuser reloadData];
}
-(NSIndexPath *)tableView :(UITableView *)theTableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(letsUserSelectRow)
return indexPath;
else
return nil;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText; // called when text changes (including clear)
{
_tbl_searchuser.hidden=FALSE;
[copylistofitem removeAllObjects];
if([searchText length] > 0)
{
searching = YES;
letsUserSelectRow = YES;
self.tbl_searchuser.scrollEnabled = YES;
[self searchtableview];
}
else
{
searching = NO;
letsUserSelectRow= NO;
[_searchbar resignFirstResponder];
//self.tbl_searchtable.scrollEnabled = NO;
}
[copylistofitem retain];
[self.tbl_searchuser reloadData];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar; // called when keyboard search button pressed
{
[self hide];
//[self searchtableview];
}
-(void)searchtableview
{
_tbl_searchuser.frame=CGRectMake(0, 65, 320, 140);
NSString *searchText = _searchbar.text;
NSLog(#"%#",array_allusers);
for(int l = 0; l < [array_allusers count]; l++)
{
NSString *tempstr = [[array_allusers objectAtIndex:l] objectForKey:#"username"];
NSRange rngstr = [tempstr rangeOfString:searchText options:(NSAnchoredSearch | NSCaseInsensitiveSearch)];
if(rngstr.length > 0)
{
[copylistofitem addObject:[array_allusers objectAtIndex:l]];
[copylistofitem retain];
}
}
NSLog(#"%#",copylistofitem);
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:#"username" ascending:YES];
[copylistofitem sortUsingDescriptors:[NSArray arrayWithObject:sort]];
[_tbl_searchuser reloadData];
}
-(void)hide
{
[_searchbar resignFirstResponder];
_tbl_searchuser.frame=CGRectMake(0, 65, 320, 274);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (searching && [_searchbar.text length] > 0)
return [copylistofitem count];
else
return yourarray;
}
NSMutableArray *copylistofitem;
BOOL searching;
BOOL letsUserSelectRow;