我传递的变量不会仅获取视频用户 ID 和个人资料图片(Swift 3)
Posted
技术标签:
【中文标题】我传递的变量不会仅获取视频用户 ID 和个人资料图片(Swift 3)【英文标题】:Variable that I'm passing doesn't fetch videos only User Id and profile image (Swift 3) 【发布时间】:2017-09-02 04:21:25 【问题描述】:我传递的变量是用户。用户通过,但当我通过 uid 时,它只显示用户名和个人资料图片。当我传递 uid 时,用户名、个人资料图片和视频应该会传递。我不知道为什么它没有得到视频。它仅显示当前用户(已登录的用户)视频。
这里的代码可以更好地理解:
用户搜索控制器:
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)
用户搜索CV:
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)
用户配置文件控制器:
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()
如果您还有其他问题,请告诉我。
【问题讨论】:
我可以看看你的数据库结构和你的用户类别吗 @iosGeek 是的,我刚刚编辑了帖子! @iOSGeek 上传帖子并正常接收,但查看其他用户时收不到。它显示当前用户的帖子 您是否使用帖子节点的处理程序从 UID 迭代和获取详细信息?您得到的结果来自节点用户。 我不这么认为/也许。如果我们进入聊天室,我可以解释我的意思,最好解释一下@iOSGeek 【参考方案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) 控制台输出结果
现在您可能需要根据从这两个数组接收到的内容来配置单元格。希望对您有所帮助 :) 如果您仍然遇到此代码实现的任何问题,请告诉我
【讨论】:
以上是关于我传递的变量不会仅获取视频用户 ID 和个人资料图片(Swift 3)的主要内容,如果未能解决你的问题,请参考以下文章