1

i can parse data and see the output also but i am not able to display them in table view

problem is 1st my tableviews code are executing then rest functions are working that's why i am getting 0 , how to avoid this ??

this is my code

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {



    NSLog(@" value %d",[jsonArray count]);// always 0
    return  [jsonArray count];


}

in this method also i am getting 0 valuee

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
   *)indexPath {  NSDictionary *aTweet = [jsonArray objectAtIndex:[indexPath
     row]];     NSLog(@ " divy is
     %@",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 all the items in to the NSArray  

   NSArray *items = (NSArray *) [menu objectForKey:@"phoneNumber"];  







    int ndx;
    //NSDictionary *stream;
    for (ndx = 0; ndx< items.count; ndx++) {

            stream = (NSDictionary *)[items objectAtIndex:ndx];


        NSLog(@"This is the title of a stream: %@", [stream valueForKey:@"type"]); 
        [jsonArray addObject:stream];


    }   

    NSLog(@" aray is %@",jsonArray);// show all data working
    NSLog(@" sterab is %@",[stream valueForKey:@"home"]);// this is also working

and this is my 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]];
}
1
  • problem is 1st my tableviews code are executing then rest functions are working thats why i am getting 0 , how to avoid this ?? Commented Sep 29, 2010 at 3:12

1 Answer 1

2

Possibly, the problem is that you data doesn't load immediately. And UITableView, when created, stores necessary data in some sort of cache. This allows to avoid often calls of data source's methods, like tableView:numberOfRowsInSection: and others. So you should explicitly call UITableView's reloadData method to refresh table.

To solve the problem, you should do something like this:

  1. In viewDidLoad just show some spinner to indicate program activity.
  2. In connectionDidFinishLoading: after all data was initialized, call [yourTableView reloadData].
Sign up to request clarification or add additional context in comments.

1 Comment

wow Awesome Yes its working perfectly :-) thanks a lot i was struggling for 2 hours . whowwwwwwwwwwwwwwwwwwww

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.