2017-02-17 9 views
1

Firebaseストレージを使用してユーザーのプロフィール写真を保存しています。これらのプロフィール写真をUITableViewに表示しようとしています。Firebase Storageから取得した後にUITableViewをリロードする場合

問題は、イメージが取得された後で、テーブルビューのデータをリロードするコードの良い点を見つけることができないことです。 tableView.reloadData()を呼び出すたびに、テーブルセルのイメージビューが空白のままであるため、イメージのダウンロードが完了する前のようです。

現在、私は、テーブルビューに表示されますユーザーを反復処理し、この関数の呼び出しでプロフィール画像を取得しています:

nearbyUsersは私のテーブルビューのデータソースと私のテーブルビューのある
func loadProfilePicture(userId: String, index: Int) { 
     // Load the stored image 
     usersRef.child(userId).observeSingleEvent(of: .value, with: { (snapshot) in 

      // Load user's profile picture from Firebase Storage if it exists (exists if the user has a profPic URL in the database) 
      if snapshot.hasChild("profilePictureUrl") { 
       self.usersStorageRef.child(userId + "/ProfilePicture").data(withMaxSize: 20*1024*1024, completion: {(data, error) in 
        let storagePicture = UIImage(data:data!) 
        // Sets the user's profile picture to the loaded image 
        self.nearbyUsers[index].profilePicture = storagePicture 
       }) 
      } else { 
       print("Error -- Loading Profile Picture") 
      } 

      DispatchQueue.main.async { 
       self.tableView.reloadData() 
      } 

     }) 
    } 

cellForRowAt機能は次のようになります。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell", for: indexPath) as! UserTableViewCell 
     let user = nearbyUsers[indexPath.row] 

     cell.labelUserName.text = user.firstName + " " + user.lastName 

     print("refreshing") 
     if user.profilePicture != nil { 
      cell.imageViewProfilePicture.image = user.profilePicture 
     } 


     return cell 
    } 

私はと私は、このような時に、現在よコードのいくつかのバリエーションを試してみた:

- それでは、どのよう私はリロードすることができ、メインスレッド

ではなく、無駄

に派遣することなく、テーブルビューのデータを-reloading各ユーザー

にloadProfilePicture関数を呼び出すループの後、テーブルビューのデータをリロードすべてのプロファイル写真がロードされていることを確認しながら、テーブルビューのデータを表示しますか?

ありがとうございます!

答えて

-1

tableViewを再読み込みする必要はありません。このライブラリでcellViewViewを設定するだけで済みます。 https://github.com/rs/SDWebImage

例:

cell.imageView.sd_setImage(with: NSURL(string:"URL")! as URL!, placeholderImage: UIImage(named:"PLACEHOLDER IMAGE"), options: [.continueInBackground, .highPriority]) { (image, error, cacheType, url) in 
         cell.imageView.image = image 
        } 
関連する問題