Last active
July 31, 2023 02:16
-
-
Save paulw11/9b89cc36ed33060497a66e42c7390e3b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// ViewController.swift | |
// DiffableTest | |
// | |
// Created by Paul Wilkinson on 28/9/20. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet weak var tableview: UITableView! | |
let identifier = "Cell" | |
var dataSource: UITableViewDiffableDataSource<Section, RowData>! = nil | |
var currentSnapshot: NSDiffableDataSourceSnapshot<Section, RowData>! = nil | |
var arrItems = [RowData]() | |
var dateFormatter: DateFormatter = { | |
let df = DateFormatter() | |
df.timeStyle = .medium | |
df.dateStyle = .short | |
return df | |
}() | |
var timer: Timer? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
for i in 0...20 { | |
arrItems.append(RowData(name: "Row \(i)")) | |
} | |
configureDataSource() | |
updateUI() | |
self.timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (timer) in | |
guard let datasource = self.tableview.dataSource as? UITableViewDiffableDataSource<Section,RowData> else { | |
return | |
} | |
var snapshot = datasource.snapshot() | |
var rowIdentifers = Set<RowData>() | |
for _ in 0...Int.random(in: 1...self.arrItems.count) { | |
let randomIndex = Int.random(in: 0...self.arrItems.count-1) | |
self.arrItems[randomIndex].timeStamp = Date() | |
rowIdentifers.insert(self.arrItems[randomIndex]) | |
} | |
snapshot.reloadItems(Array(rowIdentifers)) | |
datasource.apply(snapshot) | |
} | |
} | |
func updateUI(animated: Bool = true) { | |
currentSnapshot = NSDiffableDataSourceSnapshot<Section, RowData>() | |
currentSnapshot.appendSections([.main]) | |
currentSnapshot.appendItems(arrItems, toSection: .main) | |
self.dataSource.apply(currentSnapshot, animatingDifferences: animated) | |
} | |
func configureDataSource() { | |
self.dataSource = UITableViewDiffableDataSource | |
<Section, RowData>(tableView: tableview) { | |
(tableView: UITableView, indexPath: IndexPath, item: RowData) -> UITableViewCell? in | |
let newItem = self.arrItems[indexPath.row] | |
let cell = tableView.dequeueReusableCell( | |
withIdentifier: self.identifier, | |
for: indexPath) | |
cell.textLabel?.text = newItem.name | |
cell.detailTextLabel?.text = self.dateFormatter.string(from: newItem.timeStamp) | |
cell.backgroundColor = newItem.color | |
return cell | |
} | |
self.dataSource.defaultRowAnimation = .fade | |
} | |
} | |
enum Section: CaseIterable { | |
case main | |
} | |
struct RowData: Hashable { | |
var id: UUID = UUID() | |
var name: String | |
private let possibleColors: [UIColor] = [.yellow,.orange,.cyan] | |
dynamic var timeStamp = Date() | |
var color: UIColor { | |
return possibleColors.randomElement()! | |
} | |
func hash(into hasher: inout Hasher) { | |
hasher.combine(self.id) | |
} | |
static func ==(lhs: RowData, rhs: RowData) -> Bool { | |
return lhs.id == rhs.id | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment