views:

32

answers:

2

Hi,

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
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!

A: 

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...

Daniel
No. My problem is that reloadData only calls numberOfRowsInSection, not cellForRowAtIndexPath. When the kjeys array is filled, after numberOfRowsInSection gets called for the last time, cellForRowAtIndexPath is called.
Mihai Fonoage
not sure i follow, reloadData reloads the whole tableview, so then numberOfRows will get called first, and then it will call cellForRowatIndexPath for all the rows...
Daniel
That's the problem, cellForRowatIndexPath is not called after numberOfRowsInSection gets called, it only gets called after my keys array has been filled with the last element. I have updated the last part of my original post to better explain the situation.
Mihai Fonoage
A: 

Changing from [self performSelectorOnMainThread:@selector(updateSearchTable:) withObject:array waitUntilDone:NO]; to [self performSelectorOnMainThread:@selector(updateSearchTable:) withObject:array waitUntilDone:YES]; solved my problem.

Mihai Fonoage