This document discusses declarative user interfaces (UIs) on iOS. It begins with an overview of declarative programming and the challenges of updating UIs. It then presents a model where UI is separated into data and state. It demonstrates how to make the UI reactive to changes in data and state by finding differences between old and new values. The document outlines an architecture for declarative UIs with components for data, difference calculation, layout, and rendering. It provides examples of using this approach with SwiftUI-like declarative syntax.
10. UI = 資料 + 狀狀態
title
title
title
UI Data
{
"id": 1,
"title": "A title"
},
{
"id": 2,
"title": "A title"
},
{
"id": 3,
"title": "A title"
}
11. UI = 資料 + 狀狀態
title
loading
download
title
title
UI Data
{
"id": 1,
"title": "A title"
},
{
"id": 2,
"title": "A title"
},
{
"id": 3,
"title": "A title"
}
State
struct Item {
var isLoading: Bool
var isDownloaded:
Bool
}
12. UI = 資料 + 狀狀態
title
loading
download
title
title
func downloadBtnPressed(at indexPath: IndexPath) {
if let cell = tableView.cellForRow(
at: indexPath) as? ListTableViewCell {
cell.downloadBtn.state = .loading
stateArray[indexPath.row] = .loading
}
}
13. UI = 資料 + 狀狀態
title
loading
download
title
title
func downloadBtnPressed(at indexPath: IndexPath) {
if let cell = tableView.cellForRow(
at: indexPath) as? ListTableViewCell {
cell.downloadBtn.state = .loading
stateArray[indexPath.row] = .loading
}
}
func tableView(_ tableView: UITableView, cellForRowAt
indexPath: IndexPath) -> UITableViewCell {
cell.downloadBtn.state = stateArray[indexPath.row]
}
14. UI = 資料 + 狀狀態
title
+
download
title
title
func downloadBtnPressed(at indexPath: IndexPath) {
if let cell = tableView.cellForRow(
at: indexPath) as? ListTableViewCell {
cell.downloadBtn.state = .loading
stateArray[indexPath.row] = .loading
}
}
func tableView(_ tableView: UITableView, cellForRowAt
indexPath: IndexPath) -> UITableViewCell {
cell.downloadBtn.state = stateArray[indexPath.row]
}
97. 參參考資料
• Swift 5.1 Collection Diffing - Federico Zanetello
• A better way to update UICollectionView data in Swift
with diff framework - Khoa Pham
• Introduction to declarative UI - Flutter
• DifferenceKit - Ryo Aoyama