-1

我正在开发一个应用程序,该应用程序通过 JSON 从网络服务器获取数据到UITableViewController.

加载数据需要时间,所以我想在viewDidLoad.

这是我的第一个应用程序,所以我需要更多关于如何做的简报。

viewDidLoad方法

- (void)viewDidLoad
{
    [super viewDidLoad];


    NSString *categoryId = catId;
    NSString *post =[NSString stringWithFormat:@"categoryId=%@",categoryId];
    NSString *cat = [NSString stringWithFormat:@"&cityId=%@",cityId];
    NSString *hostStr = @"http://localhost:8888/iphone-so/bycategory.json.php?";
    hostStr = [hostStr stringByAppendingString:post];
    hostStr = [hostStr stringByAppendingString:cat];

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:hostStr]];
    NSError *error;
    productsRaw = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    products = [productsRaw objectForKey:@"products"];
    productKeys = [products allKeys];
}
4

2 回答 2

0

您可以使用MBProgressHUD,只需将 MBProgressHUD.h 和 MBProgressHUD.m 文件添加到您的项目中(如果您启用了 ARC,您可能需要转换为 ARC)

    // Add right after your [super viewDidLoad]; ] your start connection method or in viewdidload whatever....
- (void)viewDidLoad
{
    [super viewDidLoad];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"Loading...";

     // rest of the code goes here
}


// Add at start of requestFinished AND requestFailed
[MBProgressHUD hideHUDForView:self.view animated:YES];
于 2012-12-26T16:25:07.837 回答
0
- (void)viewDidLoad
{
    [super viewDidLoad];

    // create the activity indicator
    UIActivityIndicator *indicator = [UIActivityIndicator alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGrey];

    // add it
    [self.view addSubview:indicator];

    // animate it
    [indicator startAnimating];

    // your code
    NSString *categoryId = catId;
    NSString *post =[NSString stringWithFormat:@"categoryId=%@",categoryId];
    NSString *cat = [NSString stringWithFormat:@"&cityId=%@",cityId];
    NSString *hostStr = @"http://localhost:8888/iphone-so/bycategory.json.php?";
    hostStr = [hostStr stringByAppendingString:post];
    hostStr = [hostStr stringByAppendingString:cat];

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:hostStr]];
    NSError *error;
    productsRaw = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    products = [productsRaw objectForKey:@"products"];
    productKeys = [products allKeys];

    // stop animating the indicator
    [indicator stopAnimating];
}
于 2012-12-26T16:54:23.207 回答