Reputation: 886
I have a requirement that my UITableview should load data after retrieving data from web service. When retrieving huge data, my uitableview is displaying empty. Instead of that, a UIActivityIndicator should appear until data is loaded into the UITableview.
I am trying as below, but not able to get the desired functionality. Please help.
Also let me suggest what is the best approach to load data into UITableview, when data returned from webservice is huge.
Thanks in advance.
- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
UILabel *lblName = (UILabel *)[cell viewWithTag:1];
[lblName setText:[myArray objectAtIndex:[indexPath row]]];
return cell;
}
- (void)dataLoading
{
myActivityIndicator=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[myActivityIndicator setFrame:CGRectMake(140, 170, 40, 40)];
[self.view addSubview:myActivityIndicator];
[myActivityIndicator startAnimating];
NSString *playersDatalink = [NSString stringWithFormat:@"Webservice link"];
NSURL *JsonPlayersDataURL = [NSURL URLWithString:playersDatalink];
NSString *JsonPlayersData = [[NSString alloc] initWithContentsOfURL:JsonPlayersDataURL];
SBJSON *playersDataParser = [[SBJSON alloc] init];
NSDictionary *PlayersDicdata = (NSDictionary *) [playersDataParser objectWithString:JsonPlayersData error:nil];
NSDictionary *playersdata = (NSDictionary *) [PlayersDicdata objectForKey:@"players"];
NSLog(@"palyersdata is =%@",playersdata);
self.myArray = [NSMutableArray arrayWithCapacity:[playersdata count]];
for (NSDictionary *details in playersdata) {
[self.myArray addObject:[details objectForKey:@"teamid"]];
}
if ([playersdata count]==0) {
UIAlertView *myalert = [[UIAlertView alloc]initWithTitle:@"Webserive error " message:@"No data returned from webservice" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[myalert show];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelector:@selector(dataLoading) withObject:nil afterDelay:0.0];
[myActivityIndicator stopAnimating];
}
Upvotes: 0
Views: 1012
Reputation: 49720
hi Download This zip http://www.sendspace.com/file/l48jxg Unzipe this file you getting two .h and .m for loading Indicatore view,. drag and drop this into Your Project.
Now you can use this like Bellow:-
if you wish to show Activity Indicator till you data not Load:-
import LoadingView.h into your Class
,
create Object
loadingView =[LoadingView loadingViewInView:self.view:@"Please Wait.."];
at this time put this code of like olso yourTableView.userInterfaceEnable=FALSE;
and at the data Load finish method put this
[loadingView removeFromSuperview];
yourTableView.userInterfaceEnable=TRUE;
you getting loading View like:-
UPDATE
It doesn't metter your TableView delegate executing first.
1) If you Using NSURLConnection
For Parsiong WebServices you can Reload Yout TableView Data after loading Finish at:-
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//your code
[yourTableView reloadData];
}
2) if you using NSXMLParser
For Parsiong WebServices you can Reload Yout TableView Data after loading Finish at:-
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
//your code
[yourTableView reloadData];
}
Upvotes: 1
Reputation: 153
Try this Activity Indicator which stops the user interaction until your data is loaded.
use MBProgressHud available on github
Have a Happy Coding..
Upvotes: 0