2012-07-18 47 views
0

我想操縱我的UITableView中的表格單元格,因爲它從其詳細視圖返回,以提醒用戶他們最後選擇了哪一行。 UITableView是在IB中創建的,目前沒有用於UITableView或任何其他句柄的@property或自定義類。很明顯,它存在的時候,用戶從詳細視圖回來,viewWillAppear被調用,但我沒有看到我的UITableView在調試器中的任何句柄。UITableView - 從ViewWillAppear訪問代碼

任何簡單地獲取IB tableView對象句柄的方法?

// - The Containing ViewController 
    - (void)viewWillAppear:(BOOL)animated 
    { 


     [super viewWillAppear:animated]; 

     NSIndexPath *selected = [self.tableView indexPathForSelectedRow]; 

     if(selected){ 

     //Do something stylish with the last-selected row 
     NSLog(@"Last row selected was: %@",selected); 

     } 
} 

回答

0

我通過創建NSIndexPath *的類成員,並將其設置裏面didSelectRowAtIndexPath方法解決了這個。然後,當viewWillAppear執行時,當用戶導航返回fram表視圖細節時,我有最後一個選定的行,以便我可以輕鬆地突出顯示它或以任何我想要的方式處理它:

//The Containing View Controller 
- (void)viewWillAppear:(BOOL)animated 
{ 

    [super viewWillAppear:animated]; 

    if(self.lastSelectedRow){ 

     [self.tableView selectRowAtIndexPath:self.lastSelectedRow animated:YES scrollPosition:UITableViewScrollPositionNone]; 


     } 
}