Skip to content

Commit

Permalink
Merge pull request #961 from planetary-social/fix-uicollectionview-ce…
Browse files Browse the repository at this point in the history
…ll-reuse

Disable cell reuse in PagedNoteDataSource
  • Loading branch information
mplorentz authored Mar 18, 2024
2 parents 0d393ac + 4fc3374 commit a302e45
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Update font styles on Thread, Edit Profile, and Settings screens.
- Fix issue with uploading photos on Mac.
- Re-design the confirmation dialog that appears when you delete your NIP-05.
- Fixed a bug where liking a note could cause other notes to appear liked.

## [0.1.6] - 2024-03-07Z

Expand Down
18 changes: 12 additions & 6 deletions Nos/Controller/PagedNoteDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ class PagedNoteDataSource<Header: View, EmptyPlaceholder: View>: NSObject, UICol
private var emptyPlaceholder: () -> EmptyPlaceholder
let pageSize = 10

private var cellReuseID = "Cell"
private var headerReuseID = "Header"
private var footerReuseID = "Footer"
// We intentionally generate unique IDs for cell reuse to get around
// [this issue](https://github.com/planetary-social/nos/issues/873)
lazy var headerReuseID = { "Header-\(self.description)" }()
lazy var footerReuseID = { "Footer-\(self.description)" }()

init(
databaseFilter: NSFetchRequest<Event>,
Expand All @@ -42,7 +43,8 @@ class PagedNoteDataSource<Header: View, EmptyPlaceholder: View>: NSObject, UICol
self.header = header
self.emptyPlaceholder = emptyPlaceholder

collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellReuseID)
super.init()

collectionView.register(
UICollectionViewCell.self,
forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,
Expand All @@ -53,8 +55,6 @@ class PagedNoteDataSource<Header: View, EmptyPlaceholder: View>: NSObject, UICol
forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter,
withReuseIdentifier: footerReuseID
)

super.init()

self.fetchedResultsController.delegate = self

Expand Down Expand Up @@ -99,7 +99,13 @@ class PagedNoteDataSource<Header: View, EmptyPlaceholder: View>: NSObject, UICol
}

let note = fetchedResultsController.object(at: indexPath)

// We intentionally generate unique IDs for cell reuse to get around
// [this issue](https://github.com/planetary-social/nos/issues/873)
let cellReuseID = note.identifier ?? "error"
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellReuseID)
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellReuseID, for: indexPath)

cell.contentConfiguration = UIHostingConfiguration {
NoteButton(note: note, hideOutOfNetwork: false, displayRootMessage: true)
}
Expand Down

0 comments on commit a302e45

Please sign in to comment.