简体   繁体   English

cellForRowAtIndexPath调用为时已晚

[英]cellForRowAtIndexPath called too late

I am trying to re-load a table every time some data I get from the web is available. 每当从网络获取的一些数据可用时,我都试图重新加载表。 This is what I have: 这就是我所拥有的:

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];
        }
        ...     
}

My problem is that when I debug, the calls go between reloadData and numberOfRowsInSection until the keys array is filled with the last data, time at which the cellForRowAtIndexPath gets called: 我的问题是,当我调试时,调用会在reloadData和numberOfRowsInSection之间进行,直到keys数组中填充了最后一个数据,即cellForRowAtIndexPath的调用时间:

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

What I want is for the table to be updated for each element I send, one by one: 我想要的是表格针对我发送的每个元素进行更新,一个接一个:

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

Any ideas why this behavior? 任何想法为什么这种行为?

Thank you! 谢谢!

Are you asking how to load parts of the table view instad of the whole thing? 您是否在问如何加载整个表视图的一部分? If so then u can use relaodRowsAtINdexPath: or reloadSectionsAtIndexPath for this purpose, here is a reference TableView ref ... 如果是这样,那么您可以为此目的使用relaodRowsAtINdexPath:或reloadSectionsAtIndexPath,这是参考TableView ref ...

Changing from [self performSelectorOnMainThread:@selector(updateSearchTable:) withObject:array waitUntilDone:NO]; [self performSelectorOnMainThread:@selector(updateSearchTable:) withObject:array waitUntilDone:NO]; to [self performSelectorOnMainThread:@selector(updateSearchTable:) withObject:array waitUntilDone:YES]; [self performSelectorOnMainThread:@selector(updateSearchTable:) withObject:array waitUntilDone:YES]; solved my problem. 解决了我的问题。

问题出在您使用线程而不仅仅是“仅”打开或关闭标志。在不同线程上执行选择器,它将开始起作用。您还可以使用performSelectorOnBackground

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM