这是我的班级:
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?我该怎么做?
发布于 2020-10-02 02:59:37
var snapshot = DataSourceSnapshot()
SectionLayoutKind.allCases.forEach {
snapshot.appendSections([$0])
let records_copy = records
snapshot.appendItems(records_copy, toSection: $0)
}那么,让我们考虑一下代码中发生了什么。SectionLayoutKind.allCases中有两种情况,所以forEach运行了两次。
我不确定出于某种原因使用相同的数组是不好的
这并不是完全“坏”,但它肯定没有达到你想去的地方。记住,所有项目--不是同一部分的所有项目,而是所有项目--必须是唯一的。显然,如果你两次使用相同的四张唱片,那就不是唯一的了。唯一并不意味着它是相同的还是不同的对象。唯一性由单元标识符类型的可选/等效实现确定,在本例中为testRecord。从这个意义上说,您的副本与原始对象集是完全相同的,因此对于不同的数据源来说,它们是相同的。
(您没有显示testRecord类型,因此我无法进一步观察。但是,请不要再写以小写字母开头的代码。)
https://stackoverflow.com/questions/64165575
复制相似问题