0

I have followed this tutorial: http://blog.patrickcrosby.com/2010/04/27/iphone-ipad-uisearchbar-uisearchdisplaycontroller-asynchronous-example.html

In order to get my UISearchBar with SearchDisplayController. It works fine, but I don't know how make it so that when you press a suggestion from the drop down list UITableViewCells, it acts like an action and executes a method, where I can go something like:

NSString *Selected = [self SearchBar.selectedCell.text];

I am new to iPhone programming and sometimes find its quirks a tad confusing.

I have two view controllers and I'd ideally like to return the result you select from the searchDisplayController back to the initial ViewController, but just getting it to respond to a selection would be good. At the moment the cell just turns blue when you select it.

Here is my project so far: https://www.dropbox.com/s/m0zgookbwot05bo/Stocks%26Shares.zip

Click 'Search Companies' to see the UISearchBar and SearchDisplayController in its current state.

Any help would be great. If you need more info please just ask!

LOZ
  • 1,169
  • 2
  • 16
  • 43
Greg Cawthorne
  • 396
  • 5
  • 21

1 Answers1

1

In order to execute an action on table cell press you need to be implementing the UITableViewDelegate protocol in your class. Then use didSelectRowAtIndexPath to do your method. If you need to pass data between view controllers then look up the delegate pattern or, if your controllers are "close" enough to have a reference to each other then you can do [otherViewController someMethodThatDoesWhatYouWant].

Community
  • 1
  • 1
Dustin
  • 6,783
  • 4
  • 36
  • 53
  • Thanks :). I did have that UITableViewDelegate protocol added to the ViewController which contained the Search Bar with Search Display Controller, because the tutorial I followed told me to. I think the problem is with me to knowing how to use it. So do I just add this method to the .m file and when a cell is pressed it will execute it?: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{} – Greg Cawthorne Aug 12 '12 at 20:12
  • Another problem I have is that I have added a search bar and search display controller. I haven't added a UITableView myself, although the UISearchDisplayController does add one its self. How would I reference the UITableView it create on the 'fly' in my code? Do I synthesise the UISearchDisplayController, then go [self identifierOfUISearchDisplayController.TableView]; or something? – Greg Cawthorne Aug 12 '12 at 20:18
  • For your first question, yes. If you have that method in your code it's called when a cell is pressed. For number 2 it depends where you're calling it/what you want to do. – Dustin Aug 12 '12 at 20:20
  • Okay. Ideally I'd like the person to press a cell from the TableView. Then the text string of that table View to be then sent to a different ViewController. I think I may be able send the string to the other view controller, by looking at some example code, but I can't figure out how to get the text in the first place. EDIT: Okay I needed to use the cellForRowAtIndexPath method. Thanks for your help! – Greg Cawthorne Aug 12 '12 at 20:48