Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Derek Saunders
Derek Saunders
5,167 Points

Different Views for Each Cell

So I am re-creating the treehouse blog reader app for practice and for the first view that comes up when you launch the app is a table view of the following categories: Websites, Coding, iOS, Andriod and Business. Now I can't seem to figure out how to perform a segue based on the category selected. When I click on any of them each one has the same view, but I want it to have different views. Any help would be much appreciated, thanks!

6 Answers

Derek Saunders
Derek Saunders
5,167 Points

Amit Bijlani if you could please help me I would really appreciate it thanks!

Thomas Nilsen
Thomas Nilsen
14,957 Points

You need to get the indexPath to the correct cell (i.e the one that was tapped). In your prepareForSegue method, one of the arguments is (id)sender. Do something like this to get the indexPath:

NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
Derek Saunders
Derek Saunders
5,167 Points

Hey Thomas Nilsensender { if ([segue.identifier isEqualToString:@"showCategories"]) { NSIndexPath *indexPath = [self.tableView indexPathForCell:0]; [self.carSites objectAtIndex:indexPath.row]; }

}

Thomas Nilsen
Thomas Nilsen
14,957 Points

You're using storyboards. Are you absolutely 100% sure that all the segues in you storyboard are connected properly? If yes, could you provide a dropbox link to the project so I can have a look?

Derek Saunders
Derek Saunders
5,167 Points

I don't have a drop box link, but maybe I'm setting it up wrong? I select the yellow box under the view controller in the storyboard and ctrl drag to the tableview I want. In the tableview's implementation file I declare the array I want displayed. But since I'm not actually dragging it from the cell how do I tell the specific cell selected to take that segue path? Thomas Nilsen

Derek Saunders
Derek Saunders
5,167 Points

Thomas Nilsen I even tried the following code

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([indexPath row] == 0) {
        [self performSegueWithIdentifier:@"viewAndriod" sender:nil];
    }
}
Thomas Nilsen
Thomas Nilsen
14,957 Points

In my mind this should work, although I'm used to doing everything programmatically. I would really be helpful if you could zip the project and mail it to me: thnilsen90@gmail.com, or upload it to github or something :)