Search code examples
objective-cuitableviewparse-platformdidselectrowatindexpath

Presenting correct View Controller instance depending on cell selection in UITableView


I understand this might be covered in parts in answers to other questions (see references) I've seen on the site, but due to my limited experience I haven't been able to understand each part as it relates to my code. Please forgive me for any duplications.

I have a PFQueryTableViewController (essentially a UITableViewController) called threadsViewController that sources cell information from a Parse backend. The table view consists of threads similar to what you would see on a web forum.

I then have a separate class postsViewController which is another PFQueryTableViewController that I wish to display a table of all the responses ('posts') to that particular thread.

The functionality I'm looking for is for a user tapping on a thread (left screen in the image) to be presented with a postsViewController (right screen) containing only those posts/responses related to that thread. (See basic diagram below).

Desired table view action

What I do know from my research:

  • The Parse backend is established with classes for Thread and Post.
  • I'm probably going to use the didSelectRowAtIndexPath delegate method

What I need help with:

  • How can I know which thread cell a user tapped on, and then pass that on so that the postsViewController only displays posts from that Thread?
  • A layman's description of how to use indexPath.row etc

I understand how to complete PFQueries etc to get the data for the cells, I just don't know how to implement the navigation and how to tell postsViewController which posts to show.

In case it helps somehow, here is my implementation so far. I have tried addign a property to the postsViewController called fromThread to somehow store the thread but apart from that I'm out of ideas!

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];

    postsViewController *postsVC = [[postsViewController alloc] init];
    postsVC.fromThread = //?
    [self performSegueWithIdentifier:@"threadsToPosts" sender:self];
}

References

How to filter a Parse query by an tableview index?

Pass Index Number between UITableView List segue

didSelectRowAtIndexPath and prepareForSegue implementation


Solution

  • You're on the right track. You need know how to do two things: (1) access your parse objects by indexPath, and (2) push a new view controller in a navigation controller.

    (1) is simpler: PFQueryTableVC provides a method called objectAtIndexPath: that does just what you need.

    // indexPath is the indexPath parameter to the didSelectRow delegate method
    PFObject *fromThread = [self objectAtIndexPath:indexPath];
    

    (2) is simple, too. But more complicated because there are a couple ways to do it. Segue is the more modern way, but I think the old way is simpler, and certainly easier to describe in code. View Controllers are given storyboard ids on the "Identity" tab in the storyboard editor. Give your Posts-presenting VC a storyboard id like "PostVC".

    To get a new instance, use that storyboard id as follows:

    MyPostVC *postVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PostVC"];
    // initialize it with the PFObject we got above
    postVC.fromThread = fromThread;
    
    // present it on the navigation stack
    [self.navigationController pushViewController:postVC animated:YES];
    

    And fromThread is just what the PostVC will need to form a query for posts associated with the selected thread.