【问题标题】:Different cells CollectionView rxswift不同的单元格 CollectionView rxswift
【发布时间】:2019-11-19 18:51:24
【问题描述】:

晚上好。 请告诉我 我需要做这样的实现 有:CollectionView 迅捷 最大单元数为 6 如果某个数组中的元素少于 6 个,则第一个单元格填充一种类型(按数组中元素的数量),其余的填充其他单元格。

collectionView.register(UINib(nibName: "PhotoCollectionCell", bundle: nil), forCellWithReuseIdentifier: "PhotoCell")
collectionView.register(UINib(nibName: "EmptyCollectionCell", bundle: nil), forCellWithReuseIdentifier: "EmptyCell")

【问题讨论】:

    标签: uicollectionviewcell rx-cocoa


    【解决方案1】:

    请检查来自 RxCocoa 的 UICollectionView+Rx.swift 中的 collectionView.rx.items 这是代码本身的示例

       let items = Observable.just([
                 1,
                 2,
                 3
             ])
    
             items
             .bind(to: collectionView.rx.items) { (collectionView, row, element) in
                let indexPath = IndexPath(row: row, section: 0)
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! NumberCell
                 cell.value?.text = "\(element) @ \(row)"
                 return cell
             }
             .disposed(by: disposeBag)
    

    您可以根据行的索引或数据的类型将任何您想要的单元格出列,就像我们在 DataSource 中所做的那样

    【讨论】:

      【解决方案2】:

      您需要导入 RxDataSources 以防您需要不同的单元格类型。

              // TableView Cell Nib Register
              tableView.register(R.nib.meChatCell)
              tableView.register(R.nib.otherChatCell)
      
              vm.output.chats
              .bind(to: tableView.rx.items){(tv, row, item) -> UITableViewCell in
      
                  if item.userId == 0 {
                      let cellIdentifier = R.reuseIdentifier.meChatCell.identifier
                      let cell = tv.dequeueReusableCell(withIdentifier: cellIdentifier, for: IndexPath.init(row: row, section: 0)) as! MeChatCell
                      cell.vm = item
                      return cell
                  } else {
                      let cellIdentifier = R.reuseIdentifier.otherChatCell.identifier
                      let cell = tv.dequeueReusableCell(withIdentifier: cellIdentifier, for: IndexPath.init(row: row, section: 0)) as! OtherChatCell
                      cell.vm = item
                      return cell
                  }
      
              }.disposed(by: disposeBag)
      }
      

      【讨论】:

        猜你喜欢
        • 2017-09-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多