2013-11-26 65 views
0

我正在使用ECSlidingViewController来管理我的幻灯片以显示菜单。在TableView菜单中选择第一项

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

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

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

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

} 

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

任何帮助将是伟大的,谢谢!

回答

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 
} 
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]; 
    }); 
} 
相关问题