我想在我的專案中使用一個組合并面對這個問題。這是 ViewController 的代碼
import Combine
import UIKit
class ProfileDetailsController: ViewController {
//
// MARK: - Views
@IBOutlet private var tableView: UITableView!
// MARK: - Properties
private typealias DataSource = UITableViewDiffableDataSource<ProfileDetailsSection, ProfileDetailsRow>
private typealias Snapshot = NSDiffableDataSourceSnapshot<ProfileDetailsSection, ProfileDetailsRow>
@Published private var data: [ProfileDetailsSectionModel] = {
return ProfileDetailsSection.allCases.map { ProfileDetailsSectionModel(section: $0, data: $0.rows) }
}()
private lazy var dataSource: DataSource = {
let dataSource = DataSource(tableView: tableView) { tableView, _, model in
let cell = tableView.dequeueReusableCell(withIdentifier: TextFieldTableCell.name) as! TextFieldTableCell
cell.delegate = self
cell.setData(model: model)
return cell
}
dataSource.defaultRowAnimation = .fade
return dataSource
}()
}
// MARK: - Setup binding
extension ProfileDetailsController {
override func setupBinding() {
tableView.registerCellXib(cell: TextFieldTableCell.self)
$data.receive(on: RunLoop.main).sink { [weak self] models in
let sections = models.map { $0.section }
var snapshot = Snapshot()
snapshot.appendSections(sections)
models.forEach { snapshot.appendItems($0.data, toSection: $0.section) }
self?.dataSource.apply(snapshot, animatingDifferences: true)
}.store(in: &cancellable)
}
}
// MARK: - Cell delegates
extension ProfileDetailsController: TextFieldTableCellDelegate {
func switcherAction() { }
}
這是單元格的代碼。
import UIKit
protocol TextFieldTableCellData {
var placeholder: String? { get }
}
protocol TextFieldTableCellDelegate: NSObjectProtocol {
func switcherAction()
}
class TextFieldTableCell: TableViewCell {
//
// MARK: - Views
@IBOutlet private var textField: ZWTextField!
// MARK: - Properties
public weak var delegate: TextFieldTableCellDelegate?
override class var height: CGFloat {
return 72
}
}
// MARK: - Public method
extension TextFieldTableCell {
func setData(model: TextFieldTableCellData) {
textField.placeholder = model.placeholder
}
}
deinit未呼叫ViewController 。但是當我將此代碼用于 ViewController 時
import UIKit
class ProfileDetailsController: ViewController {
//
// MARK: - Views
@IBOutlet private var tableView: UITableView!
// MARK: - Properties
@Published private var data: [ProfileDetailsSectionModel] = {
return ProfileDetailsSection.allCases.map { ProfileDetailsSectionModel(section: $0, data: $0.rows) }
}()
}
// MARK: - Startup
extension ProfileDetailsController {
override func startup() {
tableView.dataSource = self
tableView.registerCellXib(cell: TextFieldTableCell.self)
}
}
// MARK: - Startup
extension ProfileDetailsController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data[section].data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let model = data[indexPath.section].data[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: TextFieldTableCell.name) as! TextFieldTableCell
cell.delegate = self
cell.setData(model: model)
return cell
}
}
// MARK: - Cell delegates
extension ProfileDetailsController: TextFieldTableCellDelegate {
func switcherAction() {}
}
一切都好。deinit叫。我嘗試將 dataSource 設定為可選并將其設定為 nil on deinit,結果相同。只有在我評論這一行時才呼叫Combine deinit:
cell.delegate = self
有誰知道這是怎么回事?Xcode 13.2 iOS 15.2
uj5u.com熱心網友回復:
聯合起來的東西是一個徹頭徹尾的紅鯡魚。這就是你無法定位問題的原因;你找錯地方了。問題是老式資料源和可差異資料源之間的區別。問題在這里:
private lazy var dataSource: DataSource = { // *
let dataSource = DataSource(tableView: tableView) { tableView, _, model in
let cell = tableView.dequeueReusableCell(withIdentifier: TextFieldTableCell.name) as! TextFieldTableCell
cell.delegate = self // *
我給有問題的臺詞加了星號:
一方面,您(
self,視圖控制器)保留了dataSource.另一方面,您為資料源提供了一個單元格提供程式函式,在該函式中您提到了
self.
這是一個保留回圈!你需要打破這個回圈。改變
let dataSource = DataSource(tableView: tableView) { tableView, _, model in
到
let dataSource = DataSource(tableView: tableView) { [weak self] tableView, _, model in
(那會編譯,因為雖然self現在是 Optional,但cell.delegate.)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/386652.html
標籤:迅速 ios15 uitableviewdiffabledatasource
上一篇:發送到連接的客戶端的最佳實踐
