1

I am trying to add Loading data option in viewDidLoad() function before [self.tableview reloaddata]. I am not sure how to add it and is there a way to make the user know that there is data getting loaded. I am parsing JSON file and the data gets loaded on 3G pretty slow, so its the better way to allow user to know that data is being loaded with loading option. Here is my code:

- (void)viewDidLoad {
  [super viewDidLoad];


// Add the view controller's view to the window and display.
responseData = [[NSMutableData data] retain];
self.twitterArray = [NSMutableArray array];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"http://search.twitter.com/search.json?q=mobtuts&rpp=5"]];


[[NSURLConnection alloc] initWithRequest:request delegate:self];   

[super viewWillAppear:animated];
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  [responseData setLength:0];
 }

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
   [responseData appendData:data];
 }


 - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];

   NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];  
   [responseData release];  

   NSDictionary *results = [responseString JSONValue];  

   self.twitterArray = [results objectForKey:@"results"]; 

   [self.tableView reloadData]; // How to add loading view before this statement

}

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   // Return the number of rows in the section.

   return [self.twitterArray count];
 }
lifemoveson
  • 1,661
  • 8
  • 28
  • 55

1 Answers1

1

I'm not quite sure what you mean by "loading view" but i guess you mean an activity indicator or something else what should be presented while you are loading data.

  1. make an ivar UIActivityIndicatorView *myLoadingView;
  2. Init it in viewDidLoad myLoadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; myLoadingView.hidesWhenStopped = YES; [myLoadingView stopAnimating]; [self.view addSubView:myLoadingView];
  3. show the view before you start your connection [myLoadingView startAnimating];
  4. hide it again when the download was finished by stoping it in the delegate method connectionDidFinishLoading: after [self.tableView reloadData]; [myLoadingView stopAnimating];
  5. release it in viewDidUnload [myLoadingView release];

Feel free to ask if you have questions or if i had misunderstood you.

yinkou
  • 5,756
  • 2
  • 24
  • 40