CombineDataSource 是一個在 GitHub 現有的第三方函式庫, 將 TableView 的DataSources 很有趣的與 Combine 結合在一起,作者 Marin Todorov 有許多關於 Swift 的教學與書籍,其中包含在知名教學網 raywenderlich 的 Combine: Asynchronous Programming with Swift。
使用 Day 24 的 SPM 方法,將 https://github.com/combineopensource/CombineDataSources 加入 CombineLoggin 專案。
override func viewDidLoad() {
super.viewDidLoad()
...
loginButton.publisher(for: .touchUpInside)
.sink { (_) in
self.passwordTextField.text = ""
let now = Date().description
self.setting.logginDate.append(now)
/*
let vc = ListViewConcroller()
vc.resourcePublisher = Just(self.setting.logginDate)
/*
self.present(vc, animated: true)
}.store(in: &set)
}
class Setting {
...
@UserDefault("logginDate", defaultValue: []) var logginDate:[String]
var logginDatePublisher:Just<[String]>{Just(self.logginDate)}
}
import CombineDataSources
class ListViewConcroller: UIViewController,Storyboarded {
@IBOutlet weak var tableView: UITableView!
var resourcePublisher : Just<[String]>!
var set = Set<AnyCancellable>()
override func viewDidLoad() {
super.viewDidLoad()
// 1
let subscriber:AnySubscriber<[String],Never> = tableView
.rowsSubscriber(cellIdentifier: "Cell",
cellType: UITableViewCell.self) { (cell, indexPath, model) in
cell.textLabel?.text = model
}
// 2
resourcePublisher.bind(subscriber: sub).store(in: &set)
}
}
loginButton.publisher(for: .touchUpInside)
.sink { (_) in
self.passwordTextField.text = ""
let now = Date().description
self.setting.logginDate.append(now)
let vc = ListViewConcroller.instantiate()
vc.resourcePublisher = self.setting.logginDatePublisher
self.present(vc, animated: true, completion: nil)
}.store(in: &set)
成果:
Source Code 於 https://github.com/ytyubox/CombineLoggin