首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DiffableDataSource CollectionView不返回节中的任何项

DiffableDataSource CollectionView不返回节中的任何项
EN

Stack Overflow用户
提问于 2020-10-02 02:43:10
回答 1查看 498关注 0票数 1

这是我的班级:

代码语言:javascript
复制
class MediaViewController: UIViewController{
    var collectionView: UICollectionView! = nil

    
    private lazy var dataSource = makeDataSource()

    fileprivate typealias DataSource = UICollectionViewDiffableDataSource<SectionLayoutKind, testRecord>

    fileprivate typealias DataSourceSnapshot = NSDiffableDataSourceSnapshot<SectionLayoutKind, testRecord>
   override func viewDidLoad() {
        super.viewDidLoad()
        setRecordItems()
        configureHierarchy()
        configureDataSource()
        applySnapshot()

    }
    
    func setRecordItems(){
        for i in 0...3{
            let record = testRecord(daysBack: i/2, progression: i/10)
            records.append(record)
        }
    }
extension MediaViewController {

    func configureHierarchy() {
        collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: createLayout())
        collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        collectionView.backgroundColor = .systemBackground
        view.addSubview(collectionView)
        collectionView.delegate = self
    }
}
extension MediaViewController {

    fileprivate enum SectionLayoutKind: Int, CaseIterable{
        case records
        case timeline
    }
    fileprivate func makeDataSource() -> DataSource {
        let dataSource = DataSource(
            collectionView: collectionView,
            cellProvider: { (collectionView, indexPath, testRecord) ->
              UICollectionViewCell? in
              // 2
                switch indexPath.section {
                case 0:
                    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: RecordCollectionViewCell.identifier, for: indexPath) as? RecordCollectionViewCell
                    cell?.configure(with: testRecord)
                    return cell
                case 1:
                    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TimelineDayCell.identifier, for: indexPath) as? TimelineDayCell
                    cell?.configure(with: testRecord)
                    return cell
                default:
                    return UICollectionViewCell()
                }
          })
          return dataSource
    }
    func configureDataSource() {
        collectionView.register(RecordCollectionViewCell.nib, forCellWithReuseIdentifier: RecordCollectionViewCell.identifier)
        collectionView.register(TimelineDayCell.nib, forCellWithReuseIdentifier: TimelineDayCell.identifier)
    }
    func applySnapshot(animatingDifferences: Bool = true) {
      // 2
      var snapshot = DataSourceSnapshot()
        SectionLayoutKind.allCases.forEach {
            snapshot.appendSections([$0])
            let records_copy = records
            snapshot.appendItems(records_copy, toSection: $0)
        }
      dataSource.apply(snapshot, animatingDifferences: animatingDifferences)
    }
}

因此,设置是有两个部分,记录和时间线。它们都由相同的数据运行--记录数组。目前,每次应用快照时,我都会从类中复制这个数组--我不确定出于某种原因使用相同的数组会有什么坏处。

然后,在设置数据源时,对于cellProvider,我有一个检查部分的开关语句。如果它的第0节将使用记录单元格,如果它的第1节,我将使用时间线单元格。

目前还没有产生记录细胞。当我检查collectionView.numberOfItems(inSection:0)时,它是0。collectionView.numberOfItems(inSection:1)为4(记录的数量)

为什么这两部分不都是4?我该怎么做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-02 02:59:37

代码语言:javascript
复制
  var snapshot = DataSourceSnapshot()
  SectionLayoutKind.allCases.forEach {
      snapshot.appendSections([$0])
      let records_copy = records
      snapshot.appendItems(records_copy, toSection: $0)
  }

那么,让我们考虑一下代码中发生了什么。SectionLayoutKind.allCases中有两种情况,所以forEach运行了两次。

  1. 第一次,我们附加一个部分,然后附加四个记录到它上。

  1. 第二次,我们追加另一个部分,然后将相同的四条记录附加到其中。这有效地删除了第一节中的四条记录,并将它们放在第二节中。

我不确定出于某种原因使用相同的数组是不好的

这并不是完全“坏”,但它肯定没有达到你想去的地方。记住,所有项目--不是同一部分的所有项目,而是所有项目--必须是唯一的。显然,如果你两次使用相同的四张唱片,那就不是唯一的了。唯一并不意味着它是相同的还是不同的对象。唯一性由单元标识符类型的可选/等效实现确定,在本例中为testRecord。从这个意义上说,您的副本与原始对象集是完全相同的,因此对于不同的数据源来说,它们是相同的。

(您没有显示testRecord类型,因此我无法进一步观察。但是,请不要再写以小写字母开头的代码。)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64165575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档