Skip to content

Instantly share code, notes, and snippets.

@auramagi
Last active August 25, 2020 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save auramagi/f6b6b4a38f502b983a0c37dd3b262a6e to your computer and use it in GitHub Desktop.
Save auramagi/f6b6b4a38f502b983a0c37dd3b262a6e to your computer and use it in GitHub Desktop.
No reloadData
// reloadDataを使わないようにしましょう
import UIKit
import DiffableDataSources // https://github.com/ra1028/DiffableDataSources
@available(iOS, introduced: 13.0)
typealias TableViewDiffableDataSource = UITableViewDiffableDataSource
@available(iOS, introduced: 13.0)
typealias DiffableDataSourceSnapshot = NSDiffableDataSourceSnapshot
class NoReloadDataTableViewController: UITableViewController {
struct SectionIdentifier: Identifiable, Hashable {
let id: Int
}
struct RowIdentifier: Identifiable, Hashable {
let id: IndexPath
}
var reloadCount: Int = 0
lazy private(set) var diffableDataSource: TableViewDiffableDataSource<SectionIdentifier, RowIdentifier> = CustomDataSource(tableView: tableView) { [weak self] (tableView, indexPath, row) -> UITableViewCell? in
guard let self = self else { return nil }
let cell = self.dequeueCell(forIndexPath: indexPath, row: row)
self.update(cell: cell, row: row)
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Remake", style: .plain, target: self, action: #selector(remakeStructure))
diffableDataSource.defaultRowAnimation = .top
remakeStructure()
}
@objc
private func remakeStructure() {
reloadCount += 1
let sectionCount = (10...50).randomElement()!
var snapshot = DiffableDataSourceSnapshot<SectionIdentifier, RowIdentifier>()
(0..<sectionCount).forEach { sectionIndex in
let section = SectionIdentifier(id: sectionIndex)
snapshot.appendSections([section])
let rowCount = (0...7).randomElement()!
let rows = (0..<rowCount).map { rowIndex in
RowIdentifier(id: IndexPath(row: rowIndex, section: sectionIndex))
}
snapshot.appendItems(rows, toSection: section)
}
diffableDataSource.apply(snapshot)
tableView.beginUpdates()
tableView.indexPathsForVisibleRows?.forEach { indexPath in
guard let cell = tableView.cellForRow(at: indexPath),
let row = diffableDataSource.itemIdentifier(for: indexPath)
else { return }
update(cell: cell, row: row)
}
tableView.endUpdates()
}
func dequeueCell(forIndexPath indexPath: IndexPath, row: RowIdentifier) -> UITableViewCell {
tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
}
func update(cell: UITableViewCell, row: RowIdentifier) {
cell.textLabel?.text = "Row \(row.id.row), section \(row.id.section). \(reloadCount)"
}
}
class CustomDataSource: TableViewDiffableDataSource<NoReloadDataTableViewController.SectionIdentifier, NoReloadDataTableViewController.RowIdentifier> {
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
"Section \(section)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment