2016-03-29 17 views
0

私の仕事は、Alamofire経由でJSONを読み込むために、私の配列を埋めて、すぐにfunc collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {の中に入れて表示しますが、私の問題はあります。空の。私は私のようにcompletionHandlerでこの問題を解決することができると考えなぜcompletionHandlerブロックは応答しませんか?

後で
func abcd(completion: (() -> Void)) { 
    let getMyProfileURL = "\(self.property.host)\(self.property.getMyProfile)" 
    Alamofire.request(.POST, getMyProfileURL, parameters: self.userParameters.profileParameteres, encoding: .JSON).responseJSON { response in 
     do { 
      let json = JSON(data: response.data!) 

      if json["user"].count > 0 { 
       self.profileDetails.append(ProfileDetailsModel(json: json["user"])) 
      } 
     } 
    } 
} 

、私はそれを呼び出す:

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { 
    self.abcd { 
     print("SUCCESS") 
     print(self.profileDetails) 
    } 
} 

が、それは私のcompletionHandlerコードprintsを印刷しません。どうして?問題は何ですか?どのように修正できますか?

P.Sあなたが私の仕事のためのより良い解決策を提供できるなら、それは素晴らしいことでしょう!

+0

は配列を初期化します。 – sourav

+0

コレクションビューをリロードする必要があります。非同期データ要求が完了したら、 –

+1

関数 'abcd'で補完ブロックを呼び出さなかったので、何も印刷されません。 – TangZijian

答えて

1

非同期データ要求が完了すると、CollectionViewをメインスレッドにリロードします。

func abcd(completion: (() -> Void)) { 
    let getMyProfileURL = "\(self.property.host)\(self.property.getMyProfile)" 
    Alamofire.request(.POST, getMyProfileURL, parameters: self.userParameters.profileParameteres, encoding: .JSON).responseJSON { response in 
     do { 
      let json = JSON(data: response.data!) 

      if json["user"].count > 0 { 
       self.profileDetails.append(ProfileDetailsModel(json: json["user"])) 
       dispatch_async(dispatch_get_main_queue()) { 
        collectionView.reloadData() 
       } 
      } 
     } 
    } 
} 
+1

ああ、私はUICollectionViewのこのメソッドについて完全に忘れてしまった!ありがとう! –

1

「なぜそれがcompletionHandlerコードを印刷していない」への応答:あなたはabcdで完了ハンドラを呼び出すことはありませんので。正しいコードは次のようになります。あなたが応答を取得し、その後collectionviewをリロードするとき

func abcd(completion: (() -> Void)) { 
let getMyProfileURL = "\(self.property.host)\(self.property.getMyProfile)" 
Alamofire.request(.POST, getMyProfileURL, parameters: self.userParameters.profileParameteres, encoding: .JSON).responseJSON { response in 
    do { 
     let json = JSON(data: response.data!) 

     if json["user"].count > 0 { 
      self.profileDetails.append(ProfileDetailsModel(json: json["user"])) 
     } 
     completion() 
    } 
} 

}

+1

と@Kumar KLは、データが取得された後、コレクションビューの再読み込みに関する正解を返します。 – TangZijian

関連する問題