Friday, 15 April 2011

ios - Fetching data from url and displaying it in a tableview -



ios - Fetching data from url and displaying it in a tableview -

in app have fetch images json webservice , display them in table view. while scrolling downwards images come right order while revert backwards images on each other. using [ tableview reloaddata] happening.

this happened me in app, have leaderboards table. because tables beingness reused.

the easiest way prepare setting cell's image nil first, downloading , displaying new image.

there other ways this, can vouch one, utilize in own app, , works great!!

here's how solve problem.

-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring * cellidentifier = @"cell"; uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (!cell) cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; // create sure set image cell nil first [cell.imageview setimage:nil]; // load new image using url , display dispatch_queue_t backgroundqueue = dispatch_queue_create("com.example.imagedownloadqueue", null); dispatch_async(backgroundqueue, ^(void){ nsurl * url = [nsurl urlwithstring:@"www.example.com"]; // url of image download uiimage * image = [uiimage imagewithdata:[nsdata datawithcontentsofurl:url]]; if (image) { dispatch_async(dispatch_get_main_queue(), ^(void) { [cell.imageview setimage:image]; // display image after downloaded }); } }); homecoming cell; }

ios

No comments:

Post a Comment