cellForRowAtIndexPath调用太迟

问题描述:

我想重新加载一个表,每次我从网上获得的一些数据是可用的。这是我有:cellForRowAtIndexPath调用太迟

 
SearchDataViewController: 

- (void)parseDataXML { 
    parsingDelegate = [[XMLParsingDelegate alloc] init];  
    parsingDelegate.searchDataController = self; 
    // CONTAINS THE TABLE THAT NEEDS RE-LOADING;  
    ImplementedSearchViewController *searchController = [[ImplementedSearchViewController alloc] initWithNibName:@"ImplementedSearchView" bundle:nil]; 

    ProjectAppDelegate *delegate = [[UIApplication sharedApplication] delegate]; 
    UINavigationController *nav = (UINavigationController *)[delegate.splitViewController.viewControllers objectAtIndex: 0]; 
    NSArray *viewControllers = [[NSArray alloc] initWithObjects:nav, searchController, nil]; 
    self.splitViewController.viewControllers = viewControllers; 
    [viewControllers release]; 
    // PASS A REFERENCE TO THE PARSING DELEGATE SO THAT IT CAN CALL reloadData on the table  
    parsingDelegate.searchViewController = searchController; 

    [searchController release]; 

    // Build the url request used to fetch data 
    ... 
    NSURLRequest *dataURLRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:dataURL]]; 
     parsingDelegate.feedConnection = [[[NSURLConnection alloc] initWithRequest:dataURLRequest delegate:parsingDelegate] autorelease]; 

} 
 

ImplementedSearchViewController: 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSLog(@"count = %d", [keys count]); 
    // keys IS A NSMutableArray 
    return [self.keys count];  
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    ... 
    cell.textLabel.text = [keys objectAtIndex:row]; 
    ... 
} 
 

XMLParsingDelgate: 

-(void) updateSearchTable:(NSArray *)array { 
    ... 
    [self.currentParseBatch addObject:(NSString *)[array objectAtIndex:1]]; 
    // RELOAD TABLE 
    [self.searchViewController.table reloadData]; 
} 
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict { 

    if ([elementName isEqualToString:@"..."]) { 
     self.currentParseBatch = [NSMutableArray array]; 
     searchViewController.keys = self.currentParseBatch; 
       ... 
    } 
     ... 
} 
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { 
    if ([elementName isEqualToString:@"..."]) { 
       ... 
     [self performSelectorOnMainThread:@selector(updateSearchTable:) withObject:array waitUntilDone:NO]; 
     } 
     ...  
} 
 

我的问题是,当我调试,呼叫reloadData和numberOfRowsInSection之间去,直到键数组充满了最后的数据,时间在其中的cellForRowAtIndexPath被称为:

 
reloadData -> numberOfRowsInSection 
reloadData -> numberOfRowsInSection 
... 
reloadData -> numberOfRowsInSection -> cellForRowAtIndexPath 

我想是该表为每个元素我送,逐一进行更新:

 
reloadData -> numberOfRowsInSection -> cellForRowAtIndexPath 
reloadData -> numberOfRowsInSection -> cellForRowAtIndexPath 
... 
reloadData -> numberOfRowsInSection -> cellForRowAtIndexPath 

任何想法,为什么这种行为?

谢谢!

改变从[self performSelectorOnMainThread:@selector(updateSearchTable:) withObject:array waitUntilDone:NO];[self performSelectorOnMainThread:@selector(updateSearchTable:) withObject:array waitUntilDone:YES];解决我的问题。

你问的是如何加载整个事物的表视图instad的一部分?如果是这样那么ü可以使用relaodRowsAtINdexPath:或reloadSectionsAtIndexPath为此,这里是一个参考TableView ref ...

+0

号我的问题是reloadData只调用numberOfRowsInSection,而不是cellForRowAtIndexPath。当kjeys数组被填充时,在最后一次调用numberOfRowsInSection之后,将调用cellForRowAtIndexPath。 – 2010-06-17 16:53:12

+0

不知道我跟着,reloadData重新加载整个tableview,所以然后numberOfRows会先调用,然后它会调用所有行的cellForRowatIndexPath ... – Daniel 2010-06-17 17:00:52

+0

这就是问题,cellForRowatIndexPath不被调用numberOfRowsInSection被调用后,它只会得到在我的keys数组已填充最后一个元素后调用。我已更新了原始文章的最后部分,以更好地解释情况。 – 2010-06-17 17:08:52

的问题是在你使用线程不“只是”在不同的线程开启标志或Off.Perform选择,它会开始work.You也可以使用performSelectorOnBackground