Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Rashii Henry
Rashii Henry
16,433 Points

Search Bar with Segmented Control for tableView?

Would anyone happen to know how i can use the UISearchBar that comes with the scopeBar (in interface Builder) to dynamically switch between 3 different prototype cells within one tableView.

I have a TableViewController with its own subclass. I've added the searchBar with the scope bar to the project.

The Goal: Im using the search bar to search through the friends, but i want the information to change when i select a certain index of the segmented control/scope bar.

What i have now: within the cellForRowAtIndexPath method I'm using a switch case to transition between the selected states of the scope bar.

switch (self.mySearchBar.selectedScopeButtonIndex)
   case 0:
self.cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
             return self.cell;

case 1:
self.cell2 = [tableView dequeueReusableCellWithIdentifier:@"Cell2" forIndexPath:indexPath];
             return self.cell2;


seems simple. However, i cant figure out a way to return a different prototype Cell depending on the selected index. I've declared the cells in the header (to make them accessible globally) but the return function will not allow me to return a specific prototype cell based on the selected index of the scope bar.