2011-04-10 23 views
0

我有一個RootViewController是UITableView的UIViewController。此RootViewController根據所選的行推送ChildViewController。當我彈出ChildViewController並返回到RootViewController時,我想能夠知道所選的indexPath。一旦我有了indexPath,我想調用selectRowAtIndexPath:animated:scrollPosition:方法將選定的indexPath滾動到頂部。我知道如何抓住tableview(indexPathForSelectedRow)中選定的indexPath,但我不知道如何保持該值,當我推動ChildViewController,然後回彈到RootViewController。如何在彈出窗口後選擇UITableView的indexPath?

回答

0

一個字段添加到您的表視圖控制器的.h文件:,

NSIndexPath* selectedIndexPath; 

    //... 
} 

//... 

@property (nonatomic, retain) NSIndexPath* selectedIndexPath; 

//... 

@end 

,然後在.m文件添加

@synthesize selectedIndexPath; 

。在此之後,使用

self.selectedIndexPath = myIndexPath; 

存儲的值,然後按如下方式使用它:

- (void)viewWillAppear:(BOOL)animated { 
     [self.tableView scrollToRowAtIndexPath:selectedIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO]; 
} 
+0

非常感謝你的快速,準確的答案。非常感激! – shawnzizzo 2011-04-10 22:23:10

0

使用靜態變量將持有你Cellselected Indexpath.row值..

爲你的類只在靜態方法或類中以.h文件聲明它,否則在執行之前@implemented

static int tableIndex = nil;

在路徑

didselect行>>>表視圖委託方法

tableindex = indexpath.row;

視圖將出現..

如果(tableIndex!=無){// 做的東西 }

感謝

相關問題