Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kikko088

macrumors member
Original poster
Oct 13, 2010
77
0
Italy
I created a subClass of UITextView with a searchBar, here is the code:

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?
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,544
6,042
It looks to me like your issue might have to do with the fact that your search bar is contained in the header row of the UITableView... thus, each time it reloads data, it destroys the search bar that is active, and then creates a new one in the exact same spot.

That's just a theory.

If that is the problem, then a possible workaround would be automatically selecting the new search bar as soon as it's created.
 

kikko088

macrumors member
Original poster
Oct 13, 2010
77
0
Italy
I have notice that I if add on the init method a view:

Code:
UITableView *newTableView = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
self.view = newTableView;
self.tableView = newTableView;

and then I add the view whene init the controller:

Code:
[self.bottomView addSubview:self.table.view];

all seems to work, in this case my doubt is only if init and add subview every time that want to change the tableview data can create memory problem.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
...in this case my doubt is only if init and add subview every time that want to change the tableview data can create memory problem.

If you're using ARC, you probably won't get a memory issue. But, initing and adding subview every time your tableview data changes certainly seems like overkill. You should look closer into how your are resigning responders when your searchBar is being used.
 

kikko088

macrumors member
Original poster
Oct 13, 2010
77
0
Italy
If you're using ARC, you probably won't get a memory issue. But, initing and adding subview every time your tableview data changes certainly seems like overkill. You should look closer into how your are resigning responders when your searchBar is being used.


for memory issue I mean a lot of memory usage, for solve the problem of re-init and re-addsub view of my table may be sufficient add

Code:
if (self.table) {
self.table=nil;
[self.table.tableview removefromsuperview];
}

before the init of my table?


kikko088
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Seems to me you are trying to cure the wrong symptom and now adding code to complicate matters. Having the keyboard dismiss properly should not require recreating entire tables. Remove that code and go back and look at the original issue closely. What is the code that is running when you enter characters in the searchBar? Use breakpoints and step-through the code, if necessary, to understand what is being executed. Using this technique you can hopefully start to hone in on the real issue.
 

kikko088

macrumors member
Original poster
Oct 13, 2010
77
0
Italy
dear dejo, my english does not allow me to explain myself well.
Originally the problem was that when I try to search a word after every character the keyboard dismiss.
In this situation I creat an UITableView on my UIVIewController and setting Delegate and Datasource of the UITableView on my UIViewController like the code below

Code:
self.table = [[SezioniTableController alloc] initWithStyle:UITableViewStylePlain andArray:self.arrayTesti];   
self.tableArgumentView.delegate = self.table;
self.tableArgumentView.dataSource = self.table;
[self.tableArgumentView reloadData];

now I do some other steps:
the first is to add a UITableView on my UITableViewController and set Delegate and DataSource of this new table directly on UITableViewController instead on UIViewController, in this situation I init theUITableViewController when I have to populate the table (not when I try to search something) and all work fine.

My doubt for re-init and re-addsubview is that the table can change data, for example, I have 5 button that do an action for populate the UITableViewController, every time I click the button I re-init the table and addsubview, I think that is better change the array of table and ReloadData of the table but not work, so I have to re-init and re-addsub view of my UITableViewController.

I hope I explained.
 

kikko088

macrumors member
Original poster
Oct 13, 2010
77
0
Italy
no no the problem has now been fixed, all work fine! :) I have only the dubt if re-init mt table every time I change the data is incorrect.


edit: no is not true that all work, after search didSelectRowAtIndexPath not work, one moment that I try to find the error :D, when I found it and correct it I update the thread :D
 
Last edited:

kikko088

macrumors member
Original poster
Oct 13, 2010
77
0
Italy
Seems to me you are trying to cure the wrong symptom and now adding code to complicate matters. Having the keyboard dismiss properly should not require recreating entire tables. Remove that code and go back and look at the original issue closely. What is the code that is running when you enter characters in the searchBar? Use breakpoints and step-through the code, if necessary, to understand what is being executed. Using this technique you can hopefully start to hone in on the real issue.

thank you, excuse me...
I think there is some problem with this table...then now I try to debug it but I want to ask your help for some input.

The table work fine unless I search something...If I search something the table was reloaded with the correct filtered array but if I click on one row didSelectRowAtIndexPath isn't called.

if I set:

self.searchDisplayController.searchResultsDelegate = self;

when I init the searchdisplaycontroller after one character uisearchbar move down and after two character kayboard was dismiss, but the row work correctly.

I try to debug the error but without good result, can you help me only with some input of where I have to see?

kikko088
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.