Swift 是否通过枚举显示多个UICollectionViewCells?
我有一个iOS应用程序,用swift编写。这是一个社交平台,用户可以发布9种不同类型的帖子:文本、图像、视频、链接、音频、投票、聊天等。我使用枚举设置这些内容 我正在使用FirebaseDatabase来存储数据。在DashboardViewController中,我查询数据库并获取阵列中的帖子以及相应的用户,准备显示Swift 是否通过枚举显示多个UICollectionViewCells?,swift,xcode,enums,uicollectionview,uicollectionviewcell,Swift,Xcode,Enums,Uicollectionview,Uicollectionviewcell,我有一个iOS应用程序,用swift编写。这是一个社交平台,用户可以发布9种不同类型的帖子:文本、图像、视频、链接、音频、投票、聊天等。我使用枚举设置这些内容 我正在使用FirebaseDatabase来存储数据。在DashboardViewController中,我查询数据库并获取阵列中的帖子以及相应的用户,准备显示 func loadPosts() { activityIndicator.startAnimating() Api.Feed.observeFee
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。例如,它会在数据库中提供文本字符串,以区分视频、照片、文本等。现在,我必须使用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
}
问题是,如何使用枚举来显示适当的单元格 在Post类中保留一个变量PostType变量。在cellForItemAt中,检查post的post类型并将相应的单元格出列
像这样的
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
}
}
如果要为每个集合视图单元格使用单独的nib文件,请确保向集合视图注册所有可能的nib,如下所示
collectionView.register(UINib(nibName: "PostTextCVC", bundle: nil), forCellWithReuseIdentifier: CellName.postTextCVC)
type返回nil,它崩溃了,我不知道为什么。它不应该返回nil,我看到值是在数据库中设置的。这是完全不同的问题,最好用细节问不同的问题。
collectionView.register(UINib(nibName: "PostTextCVC", bundle: nil), forCellWithReuseIdentifier: CellName.postTextCVC)