0

我正在使用 ECSlidingViewController 来管理我的幻灯片以显示菜单。

当我第一次滑动菜单时,没有选择任何菜单项。一旦我选择了一个项目,从这一点开始,它将保持选中状态,直到我点击另一个菜单项,如预期的那样。

当应用程序加载并且第一次滑动菜单时,如何使第一个单元格显示为选中/突出显示?

这是我尝试过的东西,但显然它总是会选择第一个单元格,所以它不好。

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

}

每次显示菜单时,都会调用此方法,然后选择第一个单元格,而不管应该选择哪个单元格。

任何帮助都会很棒,谢谢!

4

2 回答 2

0

您可以添加一个属性来跟踪选定的菜单项

@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
}
于 2013-11-26T14:34:54.617 回答
0

尝试viewDidLoad:在主线程上排队时将代码移动到。它应该只被调用一次。

- (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];
    });
}
于 2013-11-26T15:02:14.050 回答