通过枚举显示多个UICollectionViewCells?

时间:2018-06-21 12:25:08

标签: swift xcode enums uicollectionview uicollectionviewcell

我有一个快速编写的iOS应用。这是一个社交平台,用户可以在其中发布9种不同类型的帖子(文本,图像,视频,链接,音频,民意调查,聊天等)。我使用cat <<EOF > $JSONFILENAME { "config": { "encoding":"LINEAR16", "sampleRateHertz":8000, "languageCode": "nl-NL", "speechContexts": { "phrases": [''] }, "maxAlternatives": 1 }, "audio": { "content": } } EOF base64 $1 -w 0 > $SOUNDFILE.base64 #MYBASE64=$(base64 $1 -w 0) sed -i $JSONFILENAME -e "/\"content\":/r $SOUNDFILE.base64" #sed -i $JSONFILENAME -e "/\"content\":/r $MYBASE64" curl -s -X POST -H "Content-Type: application/json" --data-binary @${JSONFILENAME} https://speech.googleapis.com/v1/speech:recognize?key=$API_KEY

{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unexpected token.\n\": {\n    \"content\":\nUklGRqTIAgBXQVZFZm10\n                    ^",
    "status": "INVALID_ARGUMENT"
  }
}

我正在使用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。现在,如果是单个单元格,这很容易。我可以做到这一点,并且有效:

PostType

问题是,如何使用UICollectionViewCell显示适当的单元格?

1 个答案:

答案 0 :(得分:1)

PostType类中保留变量Post的变量。在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
    }
} 

如果您为每个集合视图单元使用单独的笔尖文件,请确保使用这样的集合视图注册所有可能的笔尖。

collectionView.register(UINib(nibName: "PostTextCVC", bundle: nil), forCellWithReuseIdentifier: CellName.postTextCVC)
相关问题