objective c - Data doesn't load in UITableView until I scroll -
i trying load parsed data in cells, problem is happening synchronously , uitableview doesn't show until data has finished loading. tried solve problem using performselectorinbackground, data isn't loaded in cells until start scrolling. here code:
- (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. [self performselectorinbackground:@selector(fethchdata) withobject:nil]; } - (void)viewdidunload { [super viewdidunload]; // release retained subviews of main view. self.listdata = nil; self.plot=nil; } -(void) fethchdata { nserror *error = nil; nsurl *url=[[nsurl alloc] initwithstring:@"http://www.website.com/"]; nsstring *strin=[[nsstring alloc] initwithcontentsofurl:url encoding:nsutf8stringencoding error:nil]; htmlparser *parser = [[htmlparser alloc] initwithstring:strin error:&error]; if (error) { nslog(@"error: %@", error); return; } listdata =[[nsmutablearray alloc] init]; plot=[[nsmutablearray alloc] init]; htmlnode *bodynode = [parser body]; nsarray *contentnodes = [bodynode findchildtags:@"p"]; (htmlnode *inputnode in contentnodes) { [plot addobject:[[inputnode allcontents] stringbytrimmingcharactersinset:[nscharacterset whitespaceandnewlinecharacterset]]]; } nsarray *divnodes = [bodynode findchildtags:@"h2"]; (htmlnode *inputnode in divnodes) { [listdata addobject:[[inputnode allcontents] stringbytrimmingcharactersinset:[nscharacterset whitespaceandnewlinecharacterset]]]; } } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; //here check precreated cell. uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier]; cell.accessorytype = uitableviewcellaccessorydisclosureindicator; } //fill cells... cell.textlabel.text = [listdata objectatindex:indexpath.row]; cell.textlabel.font = [uifont boldsystemfontofsize:14]; cell.textlabel.numberoflines=6; cell.textlabel.textcolor=[uicolor colorwithhue:0.7 saturation:1 brightness:0.4 alpha:1]; cell.detailtextlabel.text=[plot objectatindex:indexpath.row]; cell.detailtextlabel.font=[uifont systemfontofsize:11]; cell.detailtextlabel.numberoflines=6; return cell; }
put somewhere after data loaded successfully:
dispatch_async(dispatch_get_main_queue(), ^{ [self.tableview reloaddata]; }); this fix problem of calling gui update while you're not in main thread.
this code uses gcd technology apple force reload data function run on main thread. read more concurrency programming guide more understanding (it's quite large field it's hard explain in comment) anyway, it's not recommended if don't understand because causes program crash rare cases.
Comments
Post a Comment