繁体   English   中英

我传递的变量不只获取用户ID和个人资料图片(Swift 3)

[英]Variable that I'm passing doesn't fetch videos only User Id and profile image (Swift 3)

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

这就是我说仅显示当前用户视频时的意思

这是当前登录的用户

数据库的外观

这是获得更好理解的代码:

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()

    }        
}

如果您还有其他疑问,请告诉我。

在这里,我尝试过代码以得到预期的结果。 我是为您的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)控制台输出结果

在此处输入图片说明

现在您可能需要根据从这两个数组接收的内容来配置单元格。希望对您有所帮助:),并让我知道如果此代码实现仍然遇到任何问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM