我有一个 iOS 应用程序,是用 swift 编写的。这是一个社交平台,用户可以在其中发布 9 种不同类型的帖子(文本、图像、视频、链接、音频、投票、聊天等)。我使用 enum
enum PostType: String {
case image = "image"
case gif = "gif"
case video = "video"
case text = "text"
case link = "link"
case audio = "audio"
case poll = "poll"
case chat = "chat"
case quote = "quote"
}
我正在使用 FirebaseDatabase
来存储数据。在 DashboardViewController
中,我查询数据库并获取数组中的帖子以及相应的用户,准备显示。
func loadPosts() {
activityIndicator.startAnimating()
Api.Feed.observeFeedPosts(withUserId: Api.Users.CURRENT_USER!.uid) {
post in
guard let userId = post.userUid else { return }
self.fetchUser(uid: userId, completed: {
self.posts.insert(post, at: 0)
self.activityIndicator.stopAnimating()
self.collectionView.reloadData()
})
}
Api.Feed.observeFeedRemoved(withUserId: Api.Users.CURRENT_USER!.uid) { (post) in
self.posts = self.posts.filter { $0.id != post.id } // removed all array elements matching the key
self.users = self.users.filter { $0.id != post.userUid }
self.collectionView.reloadData()
}
}
func fetchUser(uid: String, completed: @escaping () -> Void ) {
Api.Users.observeUsersShort(withId: uid) {
user in
self.users.insert(user, at: 0)
completed()
}
}
每当用户创建新帖子时,它都会在数据库中存储PostType.text.rawValue
(例如,它给出“text”字符串)以区分它们(视频、照片、文本) , ETC)。现在,我必须使用 PostType
枚举来确定帖子类型并显示相应的 UICollectionViewCell
。现在,如果它是单个细胞,那就很容易了。我可以做到并且有效:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return posts.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellName.postTextCVC, for: indexPath) as! PostTextCVC
let user = users[indexPath.row]
let post = posts[indexPath.row]
cell.delegatePostTextCVC = self
cell.user = user
cell.dashboardVC = self
cell.post = post
return cell
}
问题是,如何使用enum
来显示合适的单元格?
最佳答案
在您的Post
类中保留一个变量PostType
变量。在 cellForItemAt
中检查帖子的帖子类型并出列相应的单元格。
是这样的。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let post = posts[indexPath.row]
let type: PostType = post.type // Get the post type eg. text, image etc.
switch type {
case .text:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellName.postTextCVC, for: indexPath) as! PostTextCVC
let user = users[indexPath.row]
cell.delegatePostTextCVC = self
cell.user = user
cell.dashboardVC = self
cell.post = post
return cell
case .image:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellName.postImageCVC, for: indexPath) as! PostImageCVC
return cell
case .video:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellName.postTextCVC, for: indexPath) as! PostVideoCVC
return cell
}
}
如果您为每个 Collection View 单元格使用单独的 nib 文件,请确保像这样使用 Collection View 注册所有可能的 nib。
collectionView.register(UINib(nibName: "PostTextCVC", bundle: nil), forCellWithReuseIdentifier: CellName.postTextCVC)
关于swift - 通过枚举显示多个 UICollectionViewCells?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50968535/