2017-12-23 3 views
0

サーバーからデータを取得しようとしていますが、コレクションビューにそのデータを設定しようとしています。コレクションビューは、APIを使用して取得したレスポンスを使用して作成された場合にフリーズします。

何が間違っていますか?

-(void)callService:(NSNumber*)categoryid{ 
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; 
    manager.responseSerializer = [AFJSONResponseSerializer 
            serializerWithReadingOptions:NSJSONReadingAllowFragments]; 

    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"]; 
    NSMutableDictionary *dict = [NSMutableDictionary new]; 
    [dict setValue:@1 forKey:@"page"]; 
    [dict setValue:@25 forKey:@"limit"]; 

    [dict setValue:categoryid forKey:@"category_id"]; 

    [manager POST:@"https://www.foodfuels.com/Api/getrecipes" 
     parameters:dict progress:nil success:^(NSURLSessionTask *task,id responseObject) 
    { 
     int status = [[responseObject objectForKey:@"status"] intValue]; 

     if(status == 200) 
     { 
      responseArray = [responseObject valueForKey:@"data"]; 

      dispatch_async(dispatch_get_main_queue(), ^{ 
       [MBProgressHUD hideHUDForView:self.view animated:true]; 
       [_outerCollectionView reloadData]; 

      }); 
     } 

     else 
     { 
      [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"logininfo"]; 
      UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error" message:@"some eror!" delegate:self cancelButtonTitle:Nil otherButtonTitles:@"Ok", nil]; 
      [alert show]; 
     } 
     dispatch_async(dispatch_get_main_queue(), ^{ 

      //[MBProgressHUD hideHUDForView:self.view animated:YES]; 

     }); 

    } 
      failure:^(NSURLSessionTask *operation, NSError *error) 
    {   
     UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error" message:error.localizedDescription delegate:self cancelButtonTitle:Nil otherButtonTitles:@"Ok", nil]; 

     [alert show]; 

     dispatch_async(dispatch_get_main_queue(), ^{ 

      // [MBProgressHUD hideHUDForView:self.view animated:YES]; 

     }); 

    }]; 

} 

コレクションビュー

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    if(collectionView==_headingCollectionView){ 
     recipeHeading = (RecipeHeadingCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"headingCell" forIndexPath:indexPath]; 
     _headingCollectionView.delegate = self; 
     [recipeHeading.headingBtn setTitle:[recipeTypeArray objectAtIndex:indexPath.row] forState:UIControlStateNormal]; 
     [recipeHeading.headingBtn addTarget:self action:@selector(doSomething:) forControlEvents:UIControlEventTouchUpInside]; 

     bool d = [allValues[indexPath.row] boolValue]; 

     if(d) 
     { 
      [recipeHeading.headingBtn setTitleColor:[UIColor colorWithRed:36/255.0 green:71/255.0 blue:113/255.0 alpha:1.0] forState:UIControlStateNormal];   [recipeHeading.headingBtn addSubview:bottomBorder]; 
     } 
     else 
     { 
      [recipeHeading.headingBtn setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal]; 
     } 

     return recipeHeading; 
    } 
    else if (collectionView==_outerCollectionView){ 
     RecipeOuterCell *outerCell =(RecipeOuterCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"outerCell" forIndexPath:indexPath]; 
     return outerCell; 
    } 
    else{ 
     RecipeInnerCell *innerCell = (RecipeInnerCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"innerCell" forIndexPath:indexPath]; 
     [innerCell populateRecipeScreen:responseArray index:indexPath]; 
     return innerCell; 

    } 
} 

インナーコレクションビュー電池

-(void)populateRecipeScreen:(NSArray*)recipeResponseArr index:(NSIndexPath*)path{ 
    NSLog(@"the value of recipe array are as follows %@",recipeResponseArr); 
    for(int i=0;i<recipeResponseArr.count;i++){ 
     self.comment_Count.text =[[[recipeResponseArr valueForKey:@"comment_count"]objectAtIndex:path.row]stringValue]; 
     self.titleLbl.text =[[recipeResponseArr valueForKey:@"description"]objectAtIndex:path.row]; 
     self.like_Count.text = [[[recipeResponseArr valueForKey:@"like_count"]objectAtIndex:path.row]stringValue]; 
     NSData *data= [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [[[recipeResponseArr valueForKey:@"user"]valueForKey:@"image"] objectAtIndex:path.row]]]; 
     ;   self.smallImgView.image=[UIImage imageWithData:data]; 

     self.share_Count.text = [[[recipeResponseArr valueForKey:@"share_count"]objectAtIndex:path.row]stringValue]; 
     self.subTitleLbl.text = [[[recipeResponseArr valueForKey:@"user"]valueForKey:@"username"] objectAtIndex:path.row]; 
     NSLog(@"url obtained as result %@",[[[recipeResponseArr valueForKey:@"upload_images"]valueForKey:@"name"] objectAtIndex:path.row]); 
     NSArray *urlString = [[[recipeResponseArr valueForKey:@"upload_images"]valueForKey:@"name"] objectAtIndex:path.row]; 

     NSData *mainData=[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[urlString objectAtIndex:0]]]; 
     self.mainImg.image=[UIImage imageWithData:mainData]; 
    } 
} 

答えて

2

あなたがネットワークからイメージをロードしているようにすなわちinitWithContentsOfURL:介してメインスレッドでrecipeResponseArr["user"]["image"]から(そうです。これは、強くお勧めしますドキュメントの内容:

重要

この同期メソッドを使用してネットワークベースのURLを要求しないでください。 ネットワークベースのURLの場合、この方法では低速ネットワーク上で数十秒の現在のスレッド数( )がブロックされ、ユーザーエクスペリエンスが低下し、iOSの がアプリを終了させる可能性があります。

代わりに、ファイル以外のURLの場合は、NSURLSessionクラスの dataTaskWithURL:completionHandler:メソッドの使用を検討してください。 詳細については、「URLセッションプログラミングガイド」を参照してください。

あなたがイメージをロードするための効果的かつ簡単なツールが必要な場合は、私がSDWebImageまたはAlamofireImage

を使用することをお勧めします
関連する問題