通过枚举显示多个 UICollectionViewCells?

Displaying multiple UICollectionViewCells via enum?

我有一个 iOS 应用程序,用 swift 编写。这是一个社交平台,用户可以在其中 post 9 种不同类型的 post(文本、图像、视频、link、音频、投票、聊天等)。我使用 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 中,我查询数据库并在数组中获取 posts 以及相应的用户,准备显示。

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 { [=11=].id != post.id } // removed all array elements matching the key
            self.users = self.users.filter { [=11=].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()
        }
    }

每当用户创建一个新的 post 时,它都会在数据库中存储 PostType.text.rawValue(例如,它给出 "text" 字符串)以区分它们(视频、照片、文本等)。现在,我必须使用 PostType 枚举来确定 post 类型是什么并显示相应的 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 class 中保留一个变量 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)