2009-10-19 71 views
1

我的問題似乎很簡單,但我沒有找到解決方案在這裏或其他地方。 我有一個UITableView作爲我的UIViews中的一個子類。當應用程序完成最後選擇的表格單元格被保存到NSUserDefaults,並且當應用程序重新啓動時,我想設置選定的單元格,因爲它以前。然而,當我做得太早時會導致問題,因爲部分的數目未知,即表尚未加載其數據。所以我決定在函數numberOfRowsInSection中設置它,但是我相信這不是正確的地方。UITableView何時完成更新?

- (NSInteger)tableView:(UITableView *)tableView 
    numberOfRowsInSection:(NSInteger)section 
{ 
    int iNofRows = 0; //Default 

    // It is not great doing this here but.... 
    if(bSetDefaultSelection == YES) 
    { 
     bSetDefaultSelection = NO; // Stop recursion 
     **NSIndexPath* indexPath = [NSIndexPath indexPathForRow:(NSUInteger)l last_sel_row inSection:(NSUInteger)0]; 
     [self selectRowAtIndexPath:indexPath animated:NO          scrollPosition:UITableViewScrollPositionMiddle];** 
    } 
    return iNofRows; 
} 

回答

4

我想你要找的地方

- (void)viewDidAppear:(BOOL)animated;  
// Called when the view has been fully transitioned onto the screen. Default does nothing 

(有關詳細信息,請參閱的UIViewController) 我剛剛調試我的應用程序和方法是你一直在一個叫後提到:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

在你的情況,你不得不寫類似:

- (void)viewDidAppear:(BOOL)animated{ 
    NSIndexPath* indexPath = [NSIndexPath indexPathForRow:(NSUInteger)l last_sel_row inSection:(NSUInteger)0]; 
    [self selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle]; 
} 

乾杯!

0

嘿謝謝你的回覆。我認爲沒有人對此表示讚賞。 '(void)viewDidAppear:(BOOL)動畫'的解決方案只適用於UITableViewController,但我正在處理UITableView。 但我決定發佈一個通知在我的主控制器觸發表中選擇,並正常工作。 再次感謝。