ios4 - problem in displaying JSON data in table view on iphone -
i can parse data , see output not able display them in table view
problem 1st tableviews code executing rest functions working that's why getting 0 , how avoid ??
this code
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { nslog(@" value %d",[jsonarray count]);// 0 return [jsonarray count]; } in method getting 0 valuee
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { nsdictionary *atweet = [jsonarray objectatindex:[indexpath row]]; nslog(@ " divy %@",atweet);// nothing } other codes
- (void)connectiondidfinishloading:(nsurlconnection *)connection { [connection release]; nsstring *responsestring = [[nsstring alloc] initwithdata:responsedata encoding:nsutf8stringencoding]; [responsedata release]; nserror *error; sbjson *json = [[sbjson new] autorelease]; nsdictionary *data = (nsdictionary *) [json objectwithstring:responsestring error:nil]; nsdictionary *menu = (nsdictionary *) [data objectforkey:@"name"]; //label.text=[dict objectforkey:@"firstname"]; // parsing items in nsarray nsarray *items = (nsarray *) [menu objectforkey:@"phonenumber"]; int ndx; //nsdictionary *stream; (ndx = 0; ndx< items.count; ndx++) { stream = (nsdictionary *)[items objectatindex:ndx]; nslog(@"this title of stream: %@", [stream valueforkey:@"type"]); [jsonarray addobject:stream]; } nslog(@" aray %@",jsonarray);// show data working nslog(@" sterab %@",[stream valueforkey:@"home"]);// working and view did load()
- (void)viewdidload { jsonarray= [[nsmutablearray alloc] init] ; [super viewdidload]; responsedata = [[nsmutabledata data] retain]; nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl urlwithstring:@"http://json/number.json"]]; [[nsurlconnection alloc] initwithrequest:request delegate:self]; } - (void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response { [responsedata setlength:0]; } - (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data { [responsedata appenddata:data]; } - (void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error { label.text = [nsstring stringwithformat:@"connection failed: %@", [error description]]; }
possibly, problem data doesn't load immediately. , uitableview, when created, stores necessary data in sort of cache. allows avoid calls of data source's methods, tableview:numberofrowsinsection: , others. should explicitly call uitableview's reloaddata method refresh table.
to solve problem, should this:
- in
viewdidloadshow spinner indicate program activity. - in
connectiondidfinishloading:after data initialized, call[yourtableview reloaddata].
Comments
Post a Comment