swift - 通过枚举显示多个 UICollectionViewCells?

标签 swift xcode enums uicollectionview uicollectionviewcell

我有一个 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/

相关文章:

ios - 如何使 tableViewCell Size 自动?

ios - 应用程序在iOS 10及更早版本上使用 “Could not instantiate class named _UIScrollViewLayoutGuide”崩溃

xcode - CMake GUI - 添加 XCode 生成器

objective-c - 如何在命令行中使用 xcodebuild?

swift - 如何使用 RealityKit 在零重力下生成实体?

Swift - 一些 mp3 文件元数据返回 nil

ios - ipa 或 apk 中的最大文件数

java - 用 protobuf 自动生成的枚举替换字符串枚举

c++ - 枚举类型中 const 说明符的作用是什么?

c# - 枚举值字典作为字符串