Last active
August 25, 2020 14:34
-
-
Save auramagi/f6b6b4a38f502b983a0c37dd3b262a6e to your computer and use it in GitHub Desktop.
No reloadData
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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