ios - Slow UITableView Scrolling from JSON -
my uitableview scrolls when load local json. images being loaded external url. first try loading json in viewwillappear method:
dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default,0), ^{ nsstring *filepath = [[nsbundle mainbundle] pathforresource:@"homepage" oftype:@"json"]; nserror *error = nil; nsdata *data = [nsdata datawithcontentsoffile:filepath]; self.titlelabels = [nsjsonserialization jsonobjectwithdata:data options:kniloptions error:&error]; });
and in tableview (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath, have following:
hometableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"cell"]; if(cell == nil) { [tableview registernib:[uinib nibwithnibname:@"homecell" bundle:nil] forcellreuseidentifier:@"cell"]; cell = [tableview dequeuereusablecellwithidentifier:@"cell"]; } nsdictionary *titlelabels = [self.titlelabels objectatindex:indexpath.row]; nsstring *label = [titlelabels objectforkey:@"heading"]; nsurl *imageurl = [nsurl urlwithstring:[titlelabels objectforkey:@"image"]]; uiimage *image = [uiimage imagewithdata:[nsdata datawithcontentsofurl:imageurl]]; cell.label.text = label; cell.imageview.image = image; cell.cardview.frame = cgrectmake(10, 5, 300, 395); return cell;
i wondering why table view scrolling slowly. if shed light on this, appreciated.
thanks!
uiimage *image = [uiimage imagewithdata:[nsdata datawithcontentsofurl:imageurl]]
this causing slow scrolling in app. making network call cell.imageview.image property. since calling [uiimage imagewithdata:[nsdata datawithcontentsofurl:imageurl]];
on main thread, app needs wait network call finish, can scroll farther.
the keypoint here is, doing networking calls on main thread, , touches delayed untill cells have been loaded.
this not recommended, @ all.
a better approach :
(1). make networking call, visible cells on screen. follow tutorial -- > how make faster uitableviewcell scrolling raywenderlich
(2) apple's sample code, shows in action
(3) load images beforehand , store in array, after viewdidload
Comments
Post a Comment