简体   繁体   中英

objective c table view cell drawn before data source

The data source for my UITableview cells is in:

- (void)connectionDidFinishLoading:(NSURLConnection *)conn {

    // We are just checking to make sure we are getting the XML 
    NSString *xmlCheck = [[[NSString alloc] initWithData:xmlData encoding:NSWindowsCP1252StringEncoding] autorelease];
   // NSLog(@"xmlCheck2 = %@", xmlCheck);

    TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:xmlData];
    for (int i=2; i<33; i++) {
        NSString *link=[NSString stringWithFormat:@"/html/body/table/tr[%d]/td",i];

        NSArray *elements = [xpathParser searchWithXPathQuery:link];

        NSString *date = [NSString stringWithFormat:@"%@, %@",[elements[5]text],[elements[1]text]];
        [times addObject:date];
        [names addObject:[elements[2]text]];
        [types addObject:[elements[3]text]];
        [places addObject:[elements[4]text]];

        NSLog(@"%@", elements);
        NSLog(@"%@", [elements[0] text]);
    }
}

But the method that draws the cell is called before the connection is finished even though the connection is started before I draw the cells. How do I delay the draw cell method or make sure the connection is finished before I draw the cells?

You need to set tableview's datasource & delegate properties only after you finish loading data. If you have set delegate & datasource of tableview from IB or Storyboard, remove it. Set delegate & datasource properties of tableview after you finish loading data. And reload table as well.

- (void)connectionDidFinishLoading:(NSURLConnection *)conn {

    // We are just checking to make sure we are getting the XML 
    NSString *xmlCheck = [[[NSString alloc] initWithData:xmlData encoding:NSWindowsCP1252StringEncoding] autorelease];
   // NSLog(@"xmlCheck2 = %@", xmlCheck);

    TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:xmlData];
    for (int i=2; i<33; i++) {
        NSString *link=[NSString stringWithFormat:@"/html/body/table/tr[%d]/td",i];

        NSArray *elements = [xpathParser searchWithXPathQuery:link];

        NSString *date = [NSString stringWithFormat:@"%@, %@",[elements[5]text],[elements[1]text]];
        [times addObject:date];
        [names addObject:[elements[2]text]];
        [types addObject:[elements[3]text]];
        [places addObject:[elements[4]text]];

        NSLog(@"%@", elements);
        NSLog(@"%@", [elements[0] text]);
    }

    [tableView setDelegate:self];// set delegate, datasource & reload data.
    [tableView setDatasource:self];
    [tableView reloadData];
}

that's the point of ASYNCHRONOUS networking :) Your main thread doesnt wait for it to finish! synchronous is there but it's bad

Have your UI handle the case that data isnt available yet.

a) set tableview hidden, show a spinning wheel and show and reload the table when connectionDidFinish is called

mock code

-viewWillAppear {  
    table.hidden = YES;  
    spinningActivity.hidden = NO;  
    networkConnection start];
 }

-connectionDidFinish {
    spinningActivity.hidden = YES;
    [table reloadData];
    table.hidden = NO; 
}

The method that provides cell information is only called if you tell it there are rows ready to display. If you tell it the right thing in tableView:numberOfRowsInSection: -- which could be 0 if the connection hasn't finished -- there shouldn't be any incorrect calls to tableView:cellForRowAtIndexPath: .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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