2011-02-02 108 views
0

我想在tableView上使用scrollToRowAtIndexPath。 我正在使用下面的方法。scrollToRowAtIndexPath從另一個線程

當我通過單擊UIBarButtonItem調用此方法時,一切正常,意味着 - tableView滾動就好。但是,當我從另一個線程調用這個方法時,它不會滾動。

想法?

-(void)handleDiffView 
{ 
    NSIndexPath * ndxPath; 

    if (!isLooking) { 
     ndxPath= [NSIndexPath indexPathForRow:1 inSection:0]; 
    } 
    else { 
     ndxPath= [NSIndexPath indexPathForRow:0 inSection:0]; 
    } 


    [self.tableView scrollToRowAtIndexPath:ndxPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; 

} 

回答

0

我想你也許可以做到這一點通過調用-[NSObject performSelectorOnMainThread:withObject:waitUntilDone:],像這樣:

// This will only be called from handleDiffView 
- (void)handleDiffViewInMainThread:(NSIndexPath*)ndxPath 
{ 
    [self.tableView scrollToRowAtIndexPath:ndxPath 
          atScrollPosition:UITableViewScrollPositionTop 
            animated:YES]; 
} 

- (void)handleDiffView { 
    NSIndexPath * ndxPath; 

    if (!isLooking) { 
     ndxPath= [NSIndexPath indexPathForRow:1 inSection:0]; 
    } 
    else { 
     ndxPath= [NSIndexPath indexPathForRow:0 inSection:0]; 
    } 

    [self performSelectorOnMainThread:@selector(handleDiffViewInMainThread:) 
     withObject:ndxPath waitUntilDone:NO]; 
}