0

I am using ECSlidingViewController to manage my slide to reveal menu.

When I first slide the menu over, none of the menu items are selected. Once I select an item, from this point on it will remain selected until I tap another menu item, as expected.

How can I make the first cell show as selected/highlighted when the app loads and the menu is swiped the first time?

Here is something that I tried but obviously it will ALWAYS select the first cell, so it is no good.

-(void)viewWillAppear:(BOOL)animated {
    // assuming you had the table view wired to IBOutlet myTableView
    // and that you wanted to select the first item in the first section
    [self.menuTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];

}

Each time the menu is revealed, this method is called which then selects the first cell, regardless of which cell is supposed to be selected.

Any help would be great, thanks!

Kyle Begeman
  • 7,169
  • 9
  • 40
  • 58

2 Answers2

0

You can add a property to keep track of the selected menu item

@property (nonatomic, strong) NSIndexPath *selectedIndexPath;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.selectedIndexPath = [NSIndexPath indexPathForRow:0
                                                inSection:0];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.menuTableView selectRowAtIndexPath:self.selectedIndexPath 
                                    animated:NO
                              scrollPosition:UITableViewScrollPositionNone];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedIndexPath = indexPath;
    // close menu panel and show view controller
}
Moxy
  • 4,162
  • 2
  • 30
  • 49
0

Try moving your code to viewDidLoad: while queuing it on the main thread. It should only be called once.

- (void)viewDidLoad:(BOOL)animated {
    [super viewDidLoad:animated];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.menuTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]   animated:NO scrollPosition:UITableViewScrollPositionTop];
    });
}
Michael Enriquez
  • 2,520
  • 21
  • 13