ios - ReloadData not working in my TableViewController -
good afternoon,
i'm trying display data in uitableviewcontroller (cartableviewcontroller) , @ moment rows being populate correct info , refreshcontrol working fine (and fast, 1 second) first time entry in app data not displayed (i have move screen finger , data displayed). (i have wait +15 seconds until appears automatically, not showing).
what can in order display data automatically , fast?
i tried move reloaddata every method , it's same , don't know else do...! appreciated if can me that.
cartableviewcontroller.m
#import "cartableviewcontroller.h" #import "cartableviewcell.h" #import "cartableviewcontroller.h" #import "cardetailviewcontroller.h" #import <sdwebimage/uiimageview+webcache.h> @implementation cartableviewcontroller @synthesize carmakes = _carmakes; @synthesize carmodels = _carmodels; @synthesize carimages = _carimages; @synthesize likes = _likes; @synthesize comments = _comments; @synthesize username = _username; @synthesize refuser = _refuser; @synthesize profileimage = _profileimage; - (void)viewdidload { [super viewdidload]; [self fetchjson]; [self.tableview reloaddata]; // initialize refresh control. self.refreshcontrol = [[uirefreshcontrol alloc] init]; self.refreshcontrol.backgroundcolor = [uicolor blackcolor]; self.refreshcontrol.tintcolor = [uicolor whitecolor]; [self.refreshcontrol addtarget:self action:@selector(fetchjson) forcontrolevents:uicontroleventvaluechanged]; } - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { // return number of sections. return 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { // return number of rows in section. return [_jsonarray count]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cartablecell"; cartableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[cartableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } // configure cell... cell.makelabel.text = [[_jsonarray objectatindex:indexpath.row] valueforkey:@"id"]; cell.likes.text = [[_jsonarray objectatindex:indexpath.row] valueforkey:@"likes"]; cell.comments.text = [[_jsonarray objectatindex:indexpath.row] valueforkey:@"comments"]; cell.username.text = [[_jsonarray objectatindex:indexpath.row] valueforkey:@"username"]; cell.refuser.text = [[_jsonarray objectatindex:indexpath.row] valueforkey:@"user_ref"]; cell.modellabel.text = [[_jsonarray objectatindex:indexpath.row] valueforkey:@"user"]; nsurl * imageurl = [nsurl urlwithstring:[[_jsonarray objectatindex:indexpath.row] valueforkey:@"imagen"]]; [cell.carimage setimagewithurl:imageurl placeholderimage:[uiimage imagenamed:@"placeholder.png"] options:sdwebimagerefreshcached]; nsurl * imageurl2 = [nsurl urlwithstring:[[_jsonarray objectatindex:indexpath.row] valueforkey:@"image"]]; [cell.profileimage setimagewithurl:imageurl2 placeholderimage:[uiimage imagenamed:@"image"] options:sdwebimagerefreshcached]; return cell; } -(void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { if ([[segue identifier] isequaltostring:@"showcardetails"]) { cardetailviewcontroller *detailviewcontroller = [segue destinationviewcontroller]; nsindexpath *myindexpath = [self.tableview indexpathforselectedrow]; detailviewcontroller.cardetailmodel = [[nsarray alloc] initwithobjects: [[_jsonarray objectatindex:[myindexpath row]] valueforkey:@"date"], [[_jsonarray objectatindex:[myindexpath row]] valueforkey:@"id"], [[_jsonarray objectatindex:[myindexpath row]] valueforkey:@"imagen"], nil]; } } -(void)fetchjson { dispatch_queue_t queue = dispatch_get_global_queue(dispatch_queue_priority_default, 0); dispatch_async(queue, ^{ nsstring * urlstring = [nsstring stringwithformat:@"http://mywebsite.com/service.php"]; nsurl * url = [nsurl urlwithstring:urlstring]; nsdata * data = [nsdata datawithcontentsofurl:url]; self.carmodels = [[nsmutablearray alloc] init]; self.carmakes = [[nsmutablearray alloc] init]; self.carimages = [[nsmutablearray alloc] init]; self.likes = [[nsmutablearray alloc] init]; self.comments = [[nsmutablearray alloc] init]; @try { nserror *error; [_jsonarray removeallobjects]; _jsonarray = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers|nsjsonreadingmutableleaves error:&error]; for(int i=0;i<_jsonarray.count;i++) { nsdictionary * jsonobject = [_jsonarray objectatindex:i]; nsstring* imagen = [jsonobject objectforkey:@"imagen"]; [_carimages addobject:imagen]; nsdictionary * jsonobject2 = [_jsonarray objectatindex:i]; nsstring* user = [jsonobject2 objectforkey:@"user"]; [_carmakes addobject:user]; nsdictionary * jsonobject3 = [_jsonarray objectatindex:i]; nsstring* date = [jsonobject3 objectforkey:@"date"]; [_carmodels addobject:date]; } } @catch (nsexception * e) { nslog(@"exception: %@", e); } @finally { [self.tableview reloaddata]; [self.refreshcontrol endrefreshing]; } } ); } @end
cartableviewcontroller.h
#import <uikit/uikit.h> @interface cartableviewcontroller : uitableviewcontroller @property (nonatomic, strong) iboutlet uitableview *tableview; @property (nonatomic, strong) nsmutablearray *carimages; @property (nonatomic, strong) nsmutablearray *carmakes; @property (nonatomic, strong) nsmutablearray *carmodels; @property (nonatomic, strong) nsmutablearray *likes; @property (nonatomic, strong) nsmutablearray *comments; @property (nonatomic, strong) nsmutablearray *username; @property (nonatomic, strong) nsmutablearray *refuser; @property (nonatomic, strong) nsmutablearray *profileimage; @property (nonatomic, strong) nsmutablearray *jsonarray; @end
thanks in advance.
your problem in fetchjson
. you're calling reloaddata
on background thread, has unpredictable results. need make sure call ui methods on main thread.
replace code following:
- (void)fetchjson { self.carmodels = [[nsmutablearray alloc] init]; self.carmakes = [[nsmutablearray alloc] init]; self.carimages = [[nsmutablearray alloc] init]; self.likes = [[nsmutablearray alloc] init]; self.comments = [[nsmutablearray alloc] init]; dispatch_queue_t queue = dispatch_get_global_queue(dispatch_queue_priority_default, 0); dispatch_async(queue, ^{ nsstring * urlstring = [nsstring stringwithformat:@"http://mywebsite.com/service.php"]; nsurl * url = [nsurl urlwithstring:urlstring]; nsdata * data = [nsdata datawithcontentsofurl:url]; nserror *error; _jsonarray = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers | nsjsonreadingmutableleaves error:&error]; (nsdictionary * jsonobject in _jsonarray) { nsstring* imagen = [jsonobject objectforkey:@"imagen"]; [_carimages addobject:imagen]; nsdictionary * jsonobject2 = [_jsonarray objectatindex:i]; nsstring* user = [jsonobject2 objectforkey:@"user"]; [_carmakes addobject:user]; nsdictionary * jsonobject3 = [_jsonarray objectatindex:i]; nsstring* date = [jsonobject3 objectforkey:@"date"]; [_carmodels addobject:date]; } dispatch_async(dispatch_get_main_queue(), ^{ [self.tableview reloaddata]; [self.refreshcontrol endrefreshing]; }); ); }
Comments
Post a Comment