ios - NSMutableArray is empty when reaches numberOfRowsInSection and cellForRowAtIndexPath -



ios - NSMutableArray is empty when reaches numberOfRowsInSection and cellForRowAtIndexPath -

i trying to pretty standard operation, basically, getting records form local sqlite database, getting additional info form web, merge info (which nsmutablearray) , display in table view. in viewdidload, array has required elements, in numberofrowsinsection: equals nil. because of this, cannot display items in tableview. set nil? give thanks help. code inboxviewcontroler.m

// // articleviewcontroller.m // readlater // // created ibragim gapuraev on 09/06/2014. // copyright (c) 2014 sermilion. rights reserved. // #import "inboxviewcontroller.h" #import "loginviewcontroller.h" #import "shctableviewcell.h" @interface inboxviewcontroller () @end @implementation inboxviewcontroller @synthesize db, articles, response, jsondata; - (nsmutablearray* ) articles { if (!articles) { articles = [[nsmutablearray alloc] initwithcapacity:20]; } homecoming articles; } - (database* ) db { if (!db) { db = [[database alloc] init]; } homecoming db; } //---------------------------------getting info web-------------------------------------------// /** method connect given url , send date of article has been added @ last. then, connectiondidfinishloading receive json array of articles, have been added server database after time **/ #pragma mark connection server - (void) makeconnetion:(id)data { nsmutableurlrequest * request = [nsmutableurlrequest requestwithurl:[nsurl urlwithstring:@"http://localhost/nextril/index.php"] cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:15.0]; nsurlconnection * connection = [[nsurlconnection alloc] initwithrequest:request delegate:self]; //send id of article added last, server, //which homecoming json arrya of articles id greater 1 sent [request sethttpmethod:@"post"]; [request sethttpbody:[data datausingencoding:nsutf8stringencoding]]; if (connection) { nslog(@"viewwillappear: connecting server data..."); }else{ nslog(@"viewwillappear: error while connecting..."); } } - (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata*)data { response = [[nsdata alloc] initwithdata:data]; } //check if info been received - (void) connectiondidfinishloading:(nsurlconnection *)connection { if(sizeof(response)>0){ //nslog(@"got response server %@", response); nserror* error; nsarray* json = [nsjsonserialization jsonobjectwithdata:response //1 options:kniloptions error:&error]; self.jsondata = [[nsmutablearray alloc] initwitharray:json]; int count = 0; [self.db opendatabase]; bool added = false; bool addedtouser = false; nslog(@"jsondata %d", jsondata.count); (int i=0; i<self.jsondata.count; i++) { nsdictionary *item = [self.jsondata objectatindex:i]; nsstring* content = [item objectforkey:@"content"]; nsstring* author = [item objectforkey:@"author"]; nsstring* date = [item objectforkey:@"date"]; nsstring* url = [item objectforkey:@"url"]; nsstring* tags = [item objectforkey:@"tags"]; nsinteger archived = [[item objectforkey:@"archived"]integervalue]; nsstring* title = [item objectforkey:@"title"]; //nslog(@"",); article* article = [[article alloc]initwithid:0 content:content author:author date:date url:url tags:tags arhived:archived title:title]; added = [self.db addarticletoarticledb:article]; if (added == true) { nsinteger last_id = [self.db getlastarticleid]; article.article_id = last_id; [self.articles addobject:article]; addedtouser = [self.db addarticletouserarticledb:article]; } count++; } if (added == true && addedtouser == true) { nslog(@"connectiondidfinishloading: articles has been imported. size: %d %lu", jsondata.count, (unsigned long)jsondata.count); }else{ nslog(@"connectiondidfinishloading: failed import article."); } nsarray *importedarticles = [self.db importallarticlesforuser:16 archived:0]; [self.articles addobjectsfromarray:importedarticles]; [self.db closedatabase]; }else{ nslog(@"connectiondidfinishloading: did not resopnse server: %@", response); } connection = nil; } //---------------------------------------------------------------------------------------------------- #pragma mark todo: work out why info server loads after sec login #pragma mark view - (void) viewwillappear:(bool)animated { [super viewwillappear:animated]; [self.db opendatabase]; nsstring* date_added = [self.db getlastarticledate]; [self makeconnetion:(id)date_added]; nslog(@"viewwillappear: self.articles: %d", self.articles.count); [self.db closedatabase]; } - (void)viewdidload { [super viewdidload]; self.tableview.datasource = self; self.tableview.delegate = self; self.tableview.separatorcolor = [uicolor clearcolor]; self.tableview.backgroundcolor = [uicolor blackcolor]; [self.tableview registerclass:[shctableviewcell class] forcellreuseidentifier:@"content"]; } #pragma mark - table view info source - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { // homecoming number of sections. homecoming 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { // homecoming number of rows in section. nslog(@"numberofrowsinsection: self.articles: %d", self.articles.count); homecoming self.articles.count; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"content"; shctableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath]; cell.textlabel.backgroundcolor = [uicolor clearcolor]; nsmutablearray* safearticles = self.articles; // configure cell... article* article = [safearticles objectatindex:indexpath.row]; nsstring *listingkey = article.title; nsstring *listingvalues = article.url; cell.textlabel.text = listingkey; cell.detailtextlabel.text = listingvalues ; cell.delegate = self; cell.todoitem = article; homecoming cell; } #pragma mark cell atributes -(uicolor*)colorforindex:(nsinteger) index { nsuinteger itemcount = self.articles.count - 1; float val = ((float)index / (float)itemcount) * 0.6; homecoming [uicolor colorwithred: 1.0 green:val blue: 0.0 alpha:1.0]; } -(cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { homecoming 70.0f; } -(void)tableview:(uitableview *)tableview willdisplaycell:(uitableviewcell *)cell forrowatindexpath:(nsindexpath *)indexpath { cell.backgroundcolor = [self colorforindex:indexpath.row]; } #pragma mark todo delete server database //method delete article form view , phone call method delete database, form server database -(void)deletearticle:(article*)articletodelete { . . . } #pragma mark todo delete server database //method delete article form view , phone call method delete database, form server database -(void)archivearticle:(article*)articletoarchive { . . . } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } @end

a tableview calls datasource methods populate after viewdidload. if, @ point, info source (which array) empty, nil appear in tableview. why 1 has phone call reloaddata after info source brought existence. needed in cases info source beingness fetched asyncronously.

according apple's documentation reloaddata:

call method reload info used build table, including cells, section headers , footers, index arrays, , on. efficiency, table view redisplays rows visible. adjusts offsets if table shrinks result of reload. table view's delegate or info source calls method when wants table view reload data. should not called in methods insert or delete rows, within animation block implemented calls beginupdates , endupdates

ios objective-c arrays tableview

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -