Search code examples
iosxcodeuitableviewsaslidemenu

How to set selected first cell by default in SASlideMenu?


I am using this script for slide menu "SASlideMenu" and I am trying to select the first item by default, but with no success. Does anybody know how to do it?

I tried this:

(void) viewDidLoad{
    [super viewDidLoad];

    ...

    self.tableView.delegate = self;
    NSString *CellIdentifier = @"homeCell";
    UITableViewCell *homeCell = [self.tableViewdequeueReusableCellWithIdentifier:CellIdentifier]; 
    [homeCell setSelected:YES];

}

UPDATE (improved explanation)

I am doing this in "SASlideMenuViewController.m"

UPDATE 2

Further to main question I would like to ask when user enters applicationWillEnterForeground how to select then first cell and switch to home page? P.S. I dont want to open another question for that, because it could be treated like a "duplicate".

UPDATE 3 I don't know how to switch to home page when user is on another view. For example we switch user to home page when he came back to app(foreground), but this doesn't work in next situation; in SASlideMenu I have list of categories and when I go to one of this categories and then exit from app and back again into app, it switch me to home page and that's ok. But when I click on one of categories in SASlideMenu and then click inside of category view on one of articles and exit app and enter again then it does not switch me to home page and I don't know why. It's navigation based app.


Solution

  • Question 1: To select the first item by default on launch of app

    Assuming that you are using the SASlideMenu code mentioned in the question. In your ExampleMenuViewController class you need to add the following,

    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
    
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        if (indexPath) {
            [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        }
    }
    

    __________________________________________________________________________________

    Question 2: To select the first item by default and to set home screen in applicationWillEnterForeground

    Add the following method declaration in your ExampleMenuViewController.h file,

    @interface ExampleMenuViewController :SASlideMenuViewController<SASlideMenuDataSource>
    
    - (void)selectFirstRowAndSetHomePage;
    
    @end
    

    Add the following method in your ExampleMenuViewController.m file,

    - (void)selectFirstRowAndSetHomePage {
    
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        if (indexPath) {
            [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        }
    
        NSString *identifier= [self.slideMenuDataSource initialSegueId];
        if (identifier) {
            [self performSegueWithIdentifier:identifier sender:self];
        }
    }
    

    Implement the following in your SASlideMenuAppDelegate.h class,

    #import "ExampleMenuViewController.h"
    

    And,

    - (void)applicationWillEnterForeground:(UIApplication *)application {
    
        ExampleMenuViewController *exampleMenuViewController = (ExampleMenuViewController *)self.window.rootViewController;
        [exampleMenuViewController selectFirstRowAndSetHomePage];
    }
    

    That should help.

    __________________________________________________________________________________