0

cocoapods で SVProgressHUD を使用しようとしています。オンラインの多数のチュートリアルに従って、xcodeワークスペースにインストールしました。使い方は簡単に思えますが、機能させることができません。ここで、rottentomatoes アプリからデータをロードするコードを示します。ネットワーク要求が処理されている間に「読み込み中」を表示したい。これはUIスレッドの問題なのだろうか?結果が返ってくるのが速すぎたので、ネットワーク リクエスト内にスリープを追加しました。

- (void) reload{
    [SVProgressHUD showWithStatus:@"Updating" maskType:SVProgressHUDMaskTypeBlack];

    NSString *url = @"http://api.rottentomatoes.com/api/public/v1.0/lists/dvds/top_rentals.json?";

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        [NSThread sleepForTimeInterval:3];
        NSDictionary *object = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSMutableArray *array = [[NSMutableArray alloc]initWithArray:[object objectForKey:@"movies"]];
        self.movies = [[NSMutableArray alloc]init];

        for(NSDictionary *item in array){
            SFMovie *movie = [[SFMovie alloc]initWithDictionary:item];
            [self.movies addObject:movie];

        }
                [SVProgressHUD dismiss];
        [self.tableView reloadData];
    }];
}

コメント の編集: リロードは両方の init メソッドで呼び出されます (このプロジェクトではストーリーボードを使用しているため)

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self reload];
    }
    return self;
}
- (id) initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if(self){
        [self reload];
    }
    return self;
}

2番目の編集: 更新するプルダウンを追加し、テーブルビューをプルダウンすると「更新中...」が表示されます。しかし、initには表示されません。

- (void)viewDidLoad
{
    [super viewDidLoad];


    UIRefreshControl *refreshControl = [UIRefreshControl new];
    [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
    refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to refresh..."];
    self.refreshControl = refreshControl;   
}

- (void)refresh:(UIRefreshControl *)sender{
    [self reload];
    [sender endRefreshing];
}

したがって、ビューの準備ができていないため、init メソッドで UI を実行できないと思います。現在、viewdidload で reload メソッドを呼び出しており、ファイルが機能します。ありがとうジョン!

4

1 に答える 1