Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 11, 2012, 06:31 PM   #1
kikko088
macrumors member
 
Join Date: Oct 2010
Location: Italy
kayboard hide when reload table

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?
__________________
iTouch 1G
kikko088 is offline   0 Reply With Quote
Old Nov 11, 2012, 10:41 PM   #2
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
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.
__________________
Don't tell me Macs don't last: 2007 iMac, 2007 Mac Mini, 2008 MacBook Air, all Vintage.
(iMac obsoletion: April 28, 2015, MBA: October 14, 2015, Mac Mini: March 9, 2016)
ArtOfWarfare is offline   0 Reply With Quote
Old Nov 12, 2012, 03:53 AM   #3
kikko088
Thread Starter
macrumors member
 
Join Date: Oct 2010
Location: 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.
__________________
iTouch 1G
kikko088 is offline   0 Reply With Quote
Old Nov 12, 2012, 10:00 AM   #4
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by kikko088 View Post
...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.
__________________
dejo is offline   0 Reply With Quote
Old Nov 12, 2012, 12:04 PM   #5
kikko088
Thread Starter
macrumors member
 
Join Date: Oct 2010
Location: Italy
Quote:
Originally Posted by dejo View Post
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
__________________
iTouch 1G
kikko088 is offline   0 Reply With Quote
Old Nov 12, 2012, 12:54 PM   #6
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: 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.
__________________
dejo is offline   0 Reply With Quote
Old Nov 12, 2012, 01:21 PM   #7
kikko088
Thread Starter
macrumors member
 
Join Date: Oct 2010
Location: 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.
__________________
iTouch 1G
kikko088 is offline   0 Reply With Quote
Old Nov 12, 2012, 01:38 PM   #8
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by kikko088 View Post
Originally the problem was that when I try to search a word after every character the keyboard dismiss.
Yes, and this issue has still not been corrected, right? If not, concentrate on that.
__________________
dejo is offline   0 Reply With Quote
Old Nov 12, 2012, 02:11 PM   #9
kikko088
Thread Starter
macrumors member
 
Join Date: Oct 2010
Location: 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 , when I found it and correct it I update the thread
__________________
iTouch 1G

Last edited by kikko088; Nov 12, 2012 at 02:17 PM.
kikko088 is offline   0 Reply With Quote
Old Nov 12, 2012, 02:46 PM   #10
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by kikko088 View Post
I have only the dubt if re-init mt table every time I change the data is incorrect.
Yes, I'd say that seems very incorrect and very inefficient.


Quote:
Originally Posted by kikko088 View Post
no no the problem has now been fixed...
P.S. Then, in the future, you should consider starting a separate thread for such a separate issue as your table initing concerns.
__________________
dejo is offline   0 Reply With Quote
Old Nov 12, 2012, 03:35 PM   #11
kikko088
Thread Starter
macrumors member
 
Join Date: Oct 2010
Location: Italy
Quote:
Originally Posted by dejo View Post
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
__________________
iTouch 1G

Last edited by kikko088; Nov 12, 2012 at 03:42 PM.
kikko088 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Numbers - Hide chart source/table? TonyC28 Mac Applications and Mac App Store 1 Jun 14, 2014 04:23 AM
Tableview Reload headache VolceOntra iPhone/iPad Programming 6 May 11, 2014 02:19 PM
Pause audio and reload view. mgjhowie iPhone/iPad Programming 0 Apr 7, 2014 03:32 PM
Reload iTunes Library dellavoce Mac Basics and Help 2 Dec 29, 2013 05:40 AM
iMovie reload help mck22 Digital Video 1 May 21, 2013 10:27 PM

Forum Jump

All times are GMT -5. The time now is 08:59 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC