2

我正在将此脚本用于幻灯片菜单“ SASlideMenu ”,并且我试图默认选择第一项,但没有成功。有人知道怎么做吗?

我试过这个:

(void) viewDidLoad{
    [super viewDidLoad];

    ...

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

}

更新(改进的解释)

我在“SASlideMenuViewController.m”中这样做

更新 2

关于主要问题,我想问一下当用户输入 applicationWillEnterForeground 时如何选择第一个单元格并切换到主页?PS我不想为此提出另一个问题,因为它可以被视为“重复”。

更新 3 当用户在另一个视图上时,我不知道如何切换到主页。例如,当他回到应用程序(前台)时,我们将用户切换到主页,但这在下一种情况下不起作用;在 SASlideMenu 我有类别列表,当我转到其中一个类别然后从应用程序退出并再次返回应用程序时,它会将我切换到主页,这没关系。但是,当我单击 SASlideMenu 中的一个类别,然后单击其中一篇文章的类别视图内部并退出应用程序并再次输入时,它不会将我切换到主页,我不知道为什么。它是基于导航的应用程序。

4

4 回答 4

4

问题1:应用启动时默认选择第一项

假设您正在使用SASlideMenu问题中提到的代码。在您的ExampleMenuViewController课程中,您需要添加以下内容,

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

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

_ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _ __ _ __ _ __ _ _ __ _ __ _

问题2:默认选择第一项并设置主屏幕applicationWillEnterForeground

ExampleMenuViewController.h在您的文件中添加以下方法声明,

@interface ExampleMenuViewController :SASlideMenuViewController<SASlideMenuDataSource>

- (void)selectFirstRowAndSetHomePage;

@end

ExampleMenuViewController.m在您的文件中添加以下方法,

- (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];
    }
}

SASlideMenuAppDelegate.h在您的课程中实施以下内容,

#import "ExampleMenuViewController.h"

和,

- (void)applicationWillEnterForeground:(UIApplication *)application {

    ExampleMenuViewController *exampleMenuViewController = (ExampleMenuViewController *)self.window.rootViewController;
    [exampleMenuViewController selectFirstRowAndSetHomePage];
}

那应该有帮助。

_ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _ __ _ __ _ __ _ _ __ _ __ _

于 2012-11-13T07:16:09.617 回答
1

这应该适合你:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
于 2012-11-12T15:01:23.773 回答
0

检查此代码:

NSIndexPath *indexPath = [[NSIndexPath alloc] initWithIndex:0];
UITableViewCell *homeCell = [[self.tableView cellForRowAtIndexPath:indexPath] setSelected:YES];
于 2012-11-08T11:58:17.480 回答
0

什么时候viewDidLoad叫你的桌子还是空的。您甚至没有使用表格内的单元格。

试试这个代码:

NSIndexPath firstCellIndex = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:firstCellIndex
                            animated:NO 
                      scrollPosition:UITableViewScrollPositionNone];

并将其放入viewDidAppear.

于 2012-11-08T12:10:55.380 回答