I created a subClass of UITextView with a searchBar, here is the code:
On my uiviewcontroller I create a table using the uitableview created with this code:
The viewcontroller is for an iPad application, the table and search function work perfectly, but the problem is that after every character I put in search bar the keyboard hide.
Where is the problem for you?
Code:
#import "SezioniTableController.h"
@interface SezioniTableController ()
@end
@implementation SezioniTableController
@synthesize searchBar,searchDisplayController;
@synthesize arraySezioni,arrayFiltrato;
@synthesize objTesto;
- (id)initWithStyle:(UITableViewStyle)style andArray:(NSMutableArray *)array{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
self.searchBar.delegate = self;
self.searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
self.searchDisplayController.delegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.arraySezioni = array;
self.arrayFiltrato = array;
}
return self;
}
- (void) didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) loadView {}
#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.
return self.arrayFiltrato.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:@"MyIdentifier"];
}
NSLog(@"Array count:%d",self.arraySezioni.count);
self.objTesto = [arrayFiltrato objectAtIndex:indexPath.row];
cell.textLabel.text = self.objTesto.titoloTesto;
return cell;
}
#pragma mark - Table view delegate
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *dict = [NSDictionary dictionaryWithObject:[self.arrayFiltrato objectAtIndex:indexPath.row] forKey:@"Testo"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"PassaggioTesto" object:self userInfo:dict];
NSLog(@"Click");
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
return self.searchBar;
}
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 44;
}
- (BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
NSLog(@"shouldReloadTableForSearchString");
//[[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadTable" object:self];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption{
[self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
NSLog(@"shouldReloadTableForSearchScope");
//[[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadTable" object:self];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (void) filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{
self.arrayFiltrato = [[NSMutableArray alloc] init];
NSLog(@"Array count:%d",self.arraySezioni.count);
for (objTesto in self.arraySezioni){
NSComparisonResult result = [objTesto.titoloTesto compare:searchText options:NSCaseInsensitiveSearch range:NSMakeRange(0, [searchText length])];
NSLog(@"Titolo: %@ search: %@",objTesto.titoloTesto,searchText);
if (result == NSOrderedSame){
NSLog(@"Si");
[self.arrayFiltrato addObject:objTesto];
}else{
NSLog(@"no");
}
}
NSLog(@"Array count:%d",self.arrayFiltrato.count);
}
- (void) searchBarCancelButtonClicked:(UISearchBar *)saearchBar {
[self.arrayFiltrato removeAllObjects];
self.arrayFiltrato = self.arraySezioni;
}
@end
On my uiviewcontroller I create a table using the uitableview created with this code:
Code:
int tag = [sender tag];
self.arrayTesti = [self.dataLoad leggiArgomentiDellaSezione:tag];
self.table = [[SezioniTableController alloc] initWithStyle:UITableViewStylePlain andArray:self.arrayTesti];
self.tableArgumentView.delegate = self.table;
self.tableArgumentView.dataSource = self.table;
[self.tableArgumentView reloadData];
- (void) reload {
[self.tableArgumentView reloadData];
}
The viewcontroller is for an iPad application, the table and search function work perfectly, but the problem is that after every character I put in search bar the keyboard hide.
Where is the problem for you?