0

I'm using JASidePanels for my App (Facebook menu style).

The LeftPanel is my menu (tableView) and the CenterPanel where I display my contents has a collectionView.

When I tapped an item in the center collectionView, I would also like to selected the same indexPath as the tapped item in my LeftPanel, the menu.

I tried doing this but it didn't work.

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    // Other codes ...

    LeftMenuViewController *leftMC = [[LeftMenuViewController alloc] init];
    [leftMC.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:0];
}

What should I be doing?

Thank you for your help!

1 Answers1

0

The left menu controller should already exist and most likely be visible. So, creating one is really not feasible (you would be creating it at every selection event - does not make sense).

I assume that the relationship between left menu and collection view controller is master-detail (via navigation or other controller). The usual design pattern for two controllers to "talk" to each other is a @protocol

//CollectionViewController.h
@protocol LeftMenuProtocol;

@interface ...
@property (nonatomic, assign) id<LeftMenuProtocol> delegate; 
...
@end 

@protocol LeftMenuProtocol <NSObject>
-(void)collectionView:(UICollectionView*)collectionView 
   didSelectItemAtIndexPath:(NSIndexPath*)indexPath;
@end

When creating the collection view controller, make set the left menu as its delegate. When a selection occurs, call the delegate method with self and the selected index path.

The menu can then react appropriately, e.g. by scrolling to the correct row

[self.tableView scrollToRowAtIndexPath:indexPath
  atScrollPosition:UITableViewScrollPositionMiddle 
  animated:YES];

For some general help on Protocols see Cocoa Core Competencies.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • From your answer, I understand that I'm creating a new LeftMenuViewController for each selection event in my CenterPanel, which really does not make sense. I'm sorry but I'm quite new to iOS Development so I do not really understand your code. Can your explain it a bit more? Im not sure where should I write what from the codes above. Thanks! –  Aug 30 '13 at 08:11
  • Wrong! I said creating one at each selection event is **not** feasible. That's what you are doing. -- Protocols is a very basic design pattern for iOS. Check out [Cocoa Core Competencies: Protocol](https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/Protocol.html). – Mundi Aug 30 '13 at 12:55