How to push different detail views using SearchBar

Discussion in 'iOS Programming' started by Nekbeth, Mar 14, 2011.

  1. Nekbeth macrumors member

    Nekbeth

    Joined:
    Feb 20, 2011
    Location:
    Vatican City
    #1
    I'm trying to push different detail views using the Search bar. I'm using a .plist data source. The searchbar works ok by finding the title of the text , but it always pushes the same detail view (the first row). I know that is because I have my tableview calling those detailviews using specific rows and sections. for example
    Code:
     if (indexPath.section == 0 & indexPath.row == 0){ 
    		
    		AppleTartController *apple = [[AppleTartController alloc] initWithNibName:@"AppleTartView" bundle: nil];
    		[self.navigationController pushViewController:apple animated:YES];
    		[apple release];
    		
    
    	}
    	 else if (indexPath.section == 1 & indexPath.row == 0){ 
    
    		
    		BiscottiController *biscotti = [[BiscottiController alloc] initWithNibName:@"BiscottiView" bundle: nil];
    		[self.navigationController pushViewController:biscotti animated:YES	];
    		[biscotti release];
    The Searchbar therefor always pushes the AppleTartController. I'm trying to find a logic to use inside the searchbar and push it's own nib file by it's name or any other way...

    I'm almost sure that calling the section or row won't work using Searchbar, it needs to call something more specific.. The Arrays (on the .plist) only have 1 string for the title of the recipes (searchbar finds that ok) and I'm calling their nib files by it's row or section.

    Thanks for your help
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Are you using a search bar directly? Or a UISearchBarController? If the latter then you must have an implementation for
    Code:
    - (BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
    
    In this you will be filtering your datasource based on the search criteria. You need to be using the row index on the filtered data, not the raw unfiltered data.
     
  3. Nekbeth thread starter macrumors member

    Nekbeth

    Joined:
    Feb 20, 2011
    Location:
    Vatican City
    #3

    Thanks for the reply robbieduncan, I'm still learning objective-C :( , about the SearchBar? I think I implement it directly by using the UISearchDelegate protocol inside my recipeController, then I declared the Outlets, booleans and the MutableArrays :
    Code:
    IBOutlet UISearchBar *searchBar;
    	BOOL isSearchOn;
    	BOOL canSelectRow;
    	NSMutableArray *searchResult;
    	NSMutableArray *listOfRecipes;
    	
    	NSString *CurrentTitle;
    	NSInteger CurrentLevel;
    To be honest with you, the SearchBar code is about the only thing I just copy and paste from a tutorial because it was so messy. I got the Strings from my .plist working ok, but I'm sure there must be a way to call those independent nib files when that string (text) is Selected.. Something like:
    Code:
    If (isSearchOn & [recipes ValueForKey:@"AppleTart"]) {
    
       AppleTartController *apple = [[AppleTartController alloc] initWithNibName:@"AppleTartView" bundle: nil];
    		[self.navigationController pushViewController:apple animated:YES];
    		[apple release];
    }

    I'm looking to call the string and not the row while searching, how would you do that? and could I use the method you mention with my kind of searchBar implementation? thank you.

    * please let me know if you have a link to a tutorial for implementing Search with different detail views. :)
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    I can't post a link to any tutorial as I don't read them. I think they lead to problems just like this where you don't know what the code you are using does. You are far better writing your own code that you understand.

    Anyway given the variable name I would suggest that searchResult contains the filtered row titles so when search is own use the row to index that array.
     
  5. Nekbeth thread starter macrumors member

    Nekbeth

    Joined:
    Feb 20, 2011
    Location:
    Vatican City
    #5
    Yes, I'm trying to figure out that logic.. how can I get SearchResult to contain the filter row titles ?

    About the links, your right..web tutorials confuse you more than give you answers.The thing with code that i'm realizing is that there is no 1 right way to do it, its more logic than just knowing formulas or code snippets,of course you first have to know the language :) I need a lot of training with the Search feature among many things, I will try to get it from Apple documentation itself. Thanks for your help robbie. Good day
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    I'm confused. You say:
    and then you say:
    Are you not using searchResult to serve as the datasource for your searched and filtered table data? If not, what are you using? Or perhaps, explain what you mean by: "The searchbar works ok by finding the title of the text"
     
  7. Nekbeth thread starter macrumors member

    Nekbeth

    Joined:
    Feb 20, 2011
    Location:
    Vatican City
    #7
    Hello Dejo, I think I miss understood the term "filter row titles ", if that refers to the titles of my .plist then yes, SearchResult is my database and it's working fine (here's a photo). I'm just trying to push the respective nib file to that text (or title) that the SearchBar gives me. I can push one nib file for all of them, but that's not what I'm looking for. Robbie says "when search is on use the row to index that array", I got lost right there.. I have this method to in cellForRow:
    Code:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
        static NSString *CellIdentifier = @"Cell";
        
    	
    	CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    	if (cell == nil) {
    		cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    	}
    
    	if (isSearchOn) { 
    		NSString *cellValue = [searchResult objectAtIndex:indexPath.row]; 
    		cell.textLabel.text = cellValue;
            
    		
    		
    	} else {
    	
    	//---get the category--- 
    	NSString *category = [categories objectAtIndex:[indexPath section]];
    	
    	//---get the list of recipes for that category--- 
    	NSArray *recipeSection = [recipeTitles objectForKey:category];
    	
    	//---get the particular recipe based on that row---
        cell.textLabel.text = [recipeSection objectAtIndex:[indexPath row]];
        
    	}
    	
    	
    	
    	return cell;
    }
    and for DidSelectRow I have something like this
    Code:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    	
    	[tableView deselectRowAtIndexPath:indexPath animated:YES];
    	
    	
    	
     
    	 if (indexPath.section == 0 & indexPath.row == 0){ 
    		
    		AppleTartController *apple = [[AppleTartController alloc] initWithNibName:@"AppleTartView" bundle: nil];
    		[self.navigationController pushViewController:apple animated:YES];
    		[apple release];
    		
    
    	}
    	 else if (indexPath.section == 1 & indexPath.row == 0){ 
    
    		
    		BiscottiController *biscotti = [[BiscottiController alloc] initWithNibName:@"BiscottiView" bundle: nil];
    		[self.navigationController pushViewController:biscotti animated:YES	];
    		[biscotti release];
    	
    		
    	}
    	 else if (indexPath.section == 1 & indexPath.row == 1){ 
    		 
    		 
    		 BrowniesController *brownies = [[BrowniesController alloc] initWithNibName:@"BrowniesView" bundle: nil];
    		 [self.navigationController pushViewController:brownies animated:YES	];
    		 [brownies release];
    		 
    		 
    	 }
     

    Attached Files:

    • 1.jpg
      1.jpg
      File size:
      132.4 KB
      Views:
      37
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    This piece really is the key, isn't it? (And just to be clear, it's not really a nib file that you are pushing, it's a view controller). Somehow you need to solve this problem. Here's a few approaches I would consider (in order of preferability, at least to me):

    - searchResult doesn't just hold NSStrings for row titles but model objects that encapsulates both the row title and the respective view controller

    or

    - Figure out some way to look up the corresponding view controller, perhaps using parallel arrays

    or

    - Figure out some way to construct the name of the view controller based on the row title

    Hope that helps.
     
  9. Nekbeth thread starter macrumors member

    Nekbeth

    Joined:
    Feb 20, 2011
    Location:
    Vatican City
    #9




    Thanks Dejo, indeed the problem is to call that view controller by it's name and not by it's row number. I think the Searchbar is doing everything it's supposed to do; calling the correct text from the data. It's just pushing the same view because that is how I have the didSelectRowAtIndexPath method constructed. (calling each viewController by it's section and row) . So the solution like you said, would be to figure out a way to call those view controllers by a different way, but how? my .plist is pretty simple (photo 1), it's just many arrays with many strings on each. I wonder how to call the value (text) of each string inside the .plist. It's a custom code inside didSelectRowAtIndexPath method, Do I need to create an Array first or just make an if/else statement with a condition that I'm looking for particular string "how would you write this?" (still learning objective C and this would help me a lot, thanks.

    * It's worth mention that I can't add dictionaries to my .plist (ofcourse I could but I would I have to start over my app), I know that having dictionaries enables me to add more strings and arrays which would help me call viewcontrollers by Integers inside my .plist.
     

    Attached Files:

    • 1.jpg
      1.jpg
      File size:
      31.7 KB
      Views:
      29

Share This Page