Reputation: 451
I have the following UITableViewController
+ UISearchBar
setup
@interface FeedTableView : UITableViewController <UISearchBarDelegate,UISearchDisplayDelegate>
{
NSMutableArray *ArrayDatiOriginali;
NSMutableArray *searchData;
UISearchBar *searchBar;
UISearchDisplayController *searchDisplayController;
}
Below the load method
- (void)viewDidLoad
{
[super viewDidLoad];
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate=self;
self.tableView.tableHeaderView = searchBar; tableView.
}
To select data/rows when the UISeach appear i use
if(tableView==self.searchDisplayController.searchResultsTableView)
{
NSLog(@"Riga corrente: %i",indexPath.row);
appo=[searchData objectAtIndex:indexPath.row];
}
and it works ok for filtering table view.
But if:
Then
didSelectRowAtIndexPath
is fired butin the cellForRowAtIndexPath
method the expression
if(tableView==self.searchDisplayController.searchResultsTableView) return FALSE
also if UISearchView
is active
Any ideas?
Upvotes: 2
Views: 1931
Reputation: 633
To add to the previous answer, you also need to add:
searchBar.delegate = self;
The searchBar will then be able to call
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
Upvotes: 0
Reputation: 4244
Make Two Arrays.
dataArray
& filteredDataArray
& Make a BOOL isFiltered.
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
if(text.length == 0)
{
isFiltered = FALSE;
}
else
{
isFiltered = true;
}
[self.tableView reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
isFiltered = FALSE;
[searchBar resignFirstResponder];
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(isFiltered)
{
<#YOUR_MODEL#>= [filteredDataArray objectAtIndex:indexPath.row];
}
else
{
<#YOUR_MODEL#>= [dataArray objectAtIndex:indexPath.row];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int rowCount;
if(isFiltered)
rowCount = [filteredDataArray count];
else
rowCount = [dataArray count];
return rowCount;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// [tableView deselectRowAtIndexPath:indexPath animated:YES];
if(isFiltered)
{
<#YOUR_MODEL#>= [filteredDataArray objectAtIndex:indexPath.row];
}
else
{
<#YOUR_MODEL#>= [dataArray objectAtIndex:indexPath.row];
}
//Pass it to any class or do what ever.
}
Upvotes: 6