我传递的变量不能仅提取视频用户ID和个人资料图片(Swift 3)

时间:2017-09-02 04:21:25

标签: ios iphone swift xcode firebase

我传递的变量是用户。用户通过,但只有当我通过uid时才会显示用户名和个人资料图片。当我传递uid时,用户名,个人资料图片和视频应该通过。我不知道为什么它没有收到视频。它仅显示当前用户(已登录的用户)视频。

Here's what I mean when I say it only shows current users videos

Here's the current user that's logged in

How the database looks

以下是获得更好理解的代码:

UserSearchController:

class UserSearchController: UICollectionViewController, UICollectionViewDelegateFlowLayout,UISearchBarDelegate, UISearchDisplayDelegate, GetUserSearchControllerDelegate {

let searchUsersCV: SearchUsersCV = {
    let screenHeight = UIScreen.main.bounds.height
    let screenWidth  = UIScreen.main.bounds.width
    let cv = SearchUsersCV(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))
    cv.bringSubview(toFront: cv)
    cv.collectionView.register(UserSearchCVCell.self, forCellWithReuseIdentifier: "cellId")

    return cv
}()

func searchControllerDidSelect(passedUser: User) {
    self.searchBar.isHidden = true
    searchBar.resignFirstResponder()

    let userProfileController = UserProfileController(collectionViewLayout: UICollectionViewFlowLayout())

    userProfileController.userId = passedUser.uid

    (searchUsersCV.next as? UIViewController)?.navigationController?.pushViewController(userProfileController, animated: true)

    self.navigationController?.pushViewController(userProfileController, animated: true)
}

UserSearchCV:

protocol GetUserSearchControllerDelegate {
func searchControllerDidSelect(passedUser: User)
}

class SearchUsersCV: UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,UISearchBarDelegate {

 var delegate: GetUserSearchControllerDelegate?

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    collectionView.keyboardDismissMode = .onDrag
    let user = filteredUsers[indexPath.item]

    delegate?.searchControllerDidSelect(passedUser: user)

}

UserProfileController:

class UserProfileController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

let cellId = "cellId"
var userId: String?

var user: User?

fileprivate func fetchUser() {
    let uid = userId ?? FIRAuth.auth()?.currentUser?.uid ?? ""

    FIRDatabase.fetchUserWithUid(uid: uid) { (user) in
        self.user = user

        self.navigationItem.title = self.user?.username

        self.collectionView?.reloadData()

    }        
}

如果您有任何其他问题,请告诉我。

1 个答案:

答案 0 :(得分:1)

这里我尝试过代码来获得预期的结果。我为你的userprofileController

做了这个

1)创建两个数组,如下所示

var videoUrlArray = [String]()
var thumbnailUrlArray = [String]()

2)使用以下代码替换。评论你的代码并使用我的

fileprivate func fetchOrderedPosts() {

    let ref = FIRDatabase.database().reference().child("posts").child(currentUserID)

    ref.queryOrdered(byChild: "creationDate").observe(.value, with: { (snapshot) in

        let dict = snapshot.value as! [String:AnyObject]
        print(dict)


        let dictValues = [AnyObject](dict.values)
        print(dictValues)

        for key in dictValues{

            let imageUrl = key["videoUrl"] as! String
            let imageThumbnail = key["thumbnailUrl"] as! String

            self.videoUrlArray.append(imageUrl)
            self.thumbnailUrlArray.append(imageThumbnail)

        }

            self.getResults()
    })
}

func getResults(){

    print("Thumbnail array : \(self.thumbnailUrlArray)")
    print("videoUrlArray : \(self.videoUrlArray)")

     self.collectionView?.reloadData()
}

3)控制台的输出结果

enter image description here

现在您可能需要根据从这两个阵列收到的内容配置单元格。希望它有帮助:)并告诉我如果您仍然遇到此代码实现的任何问题