2009-10-19 37 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。 但我决定发布一个通知在我的主控制器触发表中选择,并正常工作。 再次感谢。