Ios 如何防止当前用户';使用Firebase将配置文件从collectionView中显示
我的应用程序中有一个视图控制器,它通过UICollectionView显示所有用户的个人资料(他们的个人资料图片、姓名和职业)。我试图让应用程序不显示当前用户的个人资料(出于明显的原因),但我很难想出如何做到这一点 我使用Firebase获取用户的数据,因此访问当前用户的信息没有问题。我只是不确定我将如何定义这个逻辑以及它将被定义在哪里。它是在collectionView函数numberofItemsInSection或cellForItemAt下定义的吗?下面是我目前在这些函数中拥有的内容Ios 如何防止当前用户';使用Firebase将配置文件从collectionView中显示,ios,swift,firebase,firebase-authentication,uicollectionview,Ios,Swift,Firebase,Firebase Authentication,Uicollectionview,我的应用程序中有一个视图控制器,它通过UICollectionView显示所有用户的个人资料(他们的个人资料图片、姓名和职业)。我试图让应用程序不显示当前用户的个人资料(出于明显的原因),但我很难想出如何做到这一点 我使用Firebase获取用户的数据,因此访问当前用户的信息没有问题。我只是不确定我将如何定义这个逻辑以及它将被定义在哪里。它是在collectionView函数numberofItemsInSection或cellForItemAt下定义的吗?下面是我目前在这些函数中拥有的内容 f
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return users.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SearchProfileCell", for: indexPath) as! SearchProfileCell
cell.delegate = self
cell.user = users[indexPath.item]
return cell
}
在collectionview的数据源中执行此操作。构建数据源时,将
Auth.Auth().currentUser?.uid
与要添加的用户的uid
进行比较。如果它们匹配,则不要将该记录添加到数据源中。这样,您就不必更改其他功能。一种方法是在加载CollectionView之前从用户
数组中筛选出当前用户。假设您正在获取用户并将其存储在[[String:String]]
类型的数组users
(不确定,我猜是因为您没有提供完整的代码)。然后您可以创建一个struct
,如下所示:
struct FilteredUser {
let name: String
let occupation: String
let email: String
// etc...
}
let filteredUsers: [FilteredUser] = users.filter({
guard let email = $0["email"], email != UserDefaults.standard.string(forKey: "currentUserEmail") else {
return false
}
// filter by other values like name, occupation, ... if needed
}).compactMap({
guard let email = $0["email"],
let name = $0["name"], let occupation = $0["occupation"] else {
return nil
}
return FilteredUser(name: name, occupation: occupation, email: email)
})
请注意,您必须为每个用户保留一个唯一的ID,在本例中是电子邮件(有更好的选项,如uid
tho)。此外,您必须在某些地方跟踪当前用户的唯一ID,例如,您可以将当前用户的电子邮件存储在UserDefaults
中(如果您使用uid
,则更简单)。现在可以按如下方式过滤阵列:
struct FilteredUser {
let name: String
let occupation: String
let email: String
// etc...
}
let filteredUsers: [FilteredUser] = users.filter({
guard let email = $0["email"], email != UserDefaults.standard.string(forKey: "currentUserEmail") else {
return false
}
// filter by other values like name, occupation, ... if needed
}).compactMap({
guard let email = $0["email"],
let name = $0["name"], let occupation = $0["occupation"] else {
return nil
}
return FilteredUser(name: name, occupation: occupation, email: email)
})
请注意,然后您需要在数据源中提供filteredUsers