cellForRowAtIndexPath调用太晚了

时间:2010-06-17 16:45:07

标签: iphone uitableview ipad

每次我从网上获得的某些数据可用时,我都会尝试重新加载表格。这就是我所拥有的:

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之间进行,直到keys数组填充了最后一个数据,即调用cellForRowAtIndexPath的时间:

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

我想要的是逐个更新我发送的每个元素的表:

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

为什么会出现这种行为?

谢谢!

3 个答案:

答案 0 :(得分:0)

您是否在询问如何加载整个表格视图的部分内容?如果是这样,那么你可以使用relaodRowsAtINdexPath:或reloadSectionsAtIndexPath来实现这个目的,这里是一个引用TableView ref ......

答案 1 :(得分:0)

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

答案 2 :(得分:0)

问题在于你使用的线程不是“只是”打开或关闭标志。在另一个线程上执行选择器它会开始工作。你也可以使用performSelectorOnBackground