CollectionView 기준으로 작성
It provides the behavior you need to manage updates to your collection view’s data and UI in a simple, efficient way.
invalid number of sections
에러를 안 볼 수 있다.collectionView.insertItems(at: [ IndexPath(row: 0, section: 0)])
model.insert("newData.png", at: 0)
.reloadData()
를 통해서 UI를 업데이트 한다면, 뚝뚝 끊긴다. .reloadData()
나 .performBatchUpdates()
를 안 해도 된다!아무튼 좋다.
NSDiffableDataSourceSnapshot<SectionIdentifierType, ItemIdentifierType> :
@unchecked Sendable where
SectionIdentifierType : Hashable,
SectionIdentifierType : Sendable,
ItemIdentifierType : Hashable,
ItemIdentifierType : Sendable
var snapshot = NSDiffableDataSourceSnapshot<Int, UUID>()
// Populate the snapshot.
snapshot.appendSections([0])
snapshot.appendItems([UUID(), UUID(), UUID()])
// Apply the snapshot.
dataSource.apply(snapshot, animatingDifferences: true)
첫화면 | 살짝아래화면 |
---|---|
enum Section {
case newWorkSection
case genreSection
}
typealias Item = Movie
private var diffableDatasource: UICollectionViewDiffableDataSource<Section, Item>?
struct Movie: Hashable {
let id: String
let name: String
let imagePath: Int
func hash(into hasher: inout Hasher) {
hasher.combine(self.id.hashValue) // hash를 내부적으로 정의 가능
}
static func == (lhs: Self, rhs: Self) -> Bool {
lhs.hashValue == rhs.hashValue
}
hashable 프로토콜 상속 후 func hash(into:)
를 통해서 직접 지정해도 된다.
-> 서버에서 주는 id 정보로 하면 좋을 것이다.
enum Item: Hashable {
case newWork(UUID)
case movie(MoiveUUID)
}
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([.newWorkSection, genreSection])
snapshot.appendItems(viewModel.newWorks, toSection: .newWorkSection)
snapshot.appendItems(self.viewModel.movies, toSection: genre)
self.diffableDatasource?.apply(snapshot, animatingDifferences: true)
정말 편하다.
끝이 아니었다.
diffableDatasource = UICollectionViewDiffableDataSource<Section, Item>(
collectionView: collectionView,
cellProvider: { collectionView, indexPath, itemIdentifier in
if indexPath.section == 0 {
guard let cell = collectionView.dequeueReusableCell(
withReuseIdentifier: NewWorkCell.identifier,
for: indexPath
) as? NewWorkCell else {
return UICollectionViewCell()
}
cell.setData(itemIdentifier)
return cell
} else {
guard let cell = collectionView.dequeueReusableCell(
withReuseIdentifier: MovieCell.identifier,
for: indexPath
) as? MovieCell else {
return UICollectionViewCell()
}
cell.setData(itemIdentifier)
return cell
}
})
wwdc 정리 - wwdcnotes
UI Diffable Data Source란? - ellyheetov
Actor (4) - Sendable - Zedd0202