0

私のアプリでは PSCollectionView を使用しますが、デリゲート メソッドは呼び出しません。私はそうしました:

  1. JSONファイルを読みました
  2. この JSON を解析して、名前と画像の URL を取得します
  3. デバイスを画像の URL に接続して、この質問の最後にあるコードで幅と高さを取得します

この操作の後、デリゲート メソッドを呼び出して PSCollectionViewCell の数を取得し、各セルの高さを定義し、セルに画像と名前を入力する必要があります。デリゲート メソッドを実装しましたが、実行されません。私の問題は、セルの特性を作成して定義したいときに、セルを埋めるための情報を含む配列がまだ空であるためだと思います。どうすればこれを解決できますか?

コード:

- (void) loadImageFromWeb:(NSString *)urlImg forName:(NSString*)name {
    NSURL* url = [NSURL URLWithString:urlImg];
    //NSURLRequest* request = [NSURLRequest requestWithURL:url];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

    NSString *authCredentials =@"reply:reply";
    NSString *authValue = [NSString stringWithFormat:@"Basic %@",[authCredentials base64EncodedStringWithWrapWidth:0]];
    [request setValue:authValue forHTTPHeaderField:@"Authorization"];

    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse * response,
                                               NSData * data,
                                               NSError * error) {
                               if (!error){
                                   image = [[UIImage alloc] initWithData:data];
                                   imageWidth = image.size.width;
                                   imageHeight = image.size.height;
                                   imgWidth = [NSString stringWithFormat:@"%f", imageWidth];
                                   imgHeight = [NSString stringWithFormat:@"%f", imageHeight];
                                   self.dictWithDataForPSCollectionView = @{@"title": name,
                                                                            @"width": imgWidth,
                                                                            @"height": imgHeight};
                                   [self.arrayWithData addObject:self.dictWithDataForPSCollectionView];
                                   NSLog(@"DATA ARRAY: %@", self.arrayWithData);
                               } else {
                                   NSLog(@"ERRORE: %@", error);
                               }

                           }];
}

誰でもこの問題を解決するのを手伝ってもらえますか? ありがとうございました!

4

2 に答える 2

1

あなたの問題は、そのように配列を設定するときにデータをリロードする必要がある問題を修正するために配列を設定する前に、デリゲートとデータソースが呼び出されることです。

-(void)setArrayWithData:(NSArray *)arrayWithData{

    if(_arrayWithData != arrayWithData){
       _arrayWithData = arrayWithData;
    }

    [_yourCollectionView reloadData];

}
于 2013-11-11T13:43:46.040 に答える