gpt4 book ai didi

swift - 如何将演示者注入(inject)到自定义 uiTableViewCell 作为依赖注入(inject)

转载 作者:行者123 更新时间:2023-11-30 10:40:24 26 4
gpt4 key购买 nike

我正在学习依赖注入(inject)并通过 MVP 创建了一个应用程序。

我可以使用 Storyboard在 AppDelegate 中注入(inject)一些 VC 的演示者。

但现在我使用 .xib 创建了 CustomTableViewCell ,该单元格上有 UIButton ,以便我想在其中处理它的详细信息演示者文件。

我尝试在AppDelegate中创建UINib(nibName~),但是当CustomTableViewCell中的presenter被调用时,它是nil.

我听说“AppDelegate”中调用的单元格实例将被丢弃。但是我不知道有什么方法可以不让 presenter 为零。

应用程序委托(delegate)

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

let mainTabVC = UIStoryboard(name: "MainTab", bundle: nil).instantiateInitialViewController() as! MainTabViewController
let userDefault = UserDefault()
let presenter = MainTabPresenter(view: mainTabVC, udManager: userDefault)
mainTabVC.inject(presenter: presenter, userDefaultManager: userDefault)

let addTabVC = UIStoryboard(name: "AddTab", bundle: nil).instantiateInitialViewController() as! AddTabViewController
let alertHandller = AlertHandller()
let addPresenter = AddTabPresenter(view: addTabVC, mainView: mainTabVC, alertHandller: alertHandller)
addTabVC.inject(presenter: addPresenter)

let settingTabVC = UIStoryboard(name: "SettingTab", bundle: nil).instantiateInitialViewController() as! SettingTabViewController
let settingPresenter = SettingTabPresenter(view: settingTabVC)
settingTabVC.inject(presenter: settingPresenter, alertHandller: alertHandller)

let vcs = [mainTabVC, addTabVC, settingTabVC]

let mainTabBar = UIStoryboard(name: "MainView", bundle: nil).instantiateInitialViewController() as! MainTabBarController
mainTabBar.setViewControllers(vcs, animated: false)

// no probs until here (inject functions work)

// this is disposed??
let listCell = UINib(nibName: "ListCell", bundle: nil).instantiate(withOwner: ListCell.self, options: nil).first as! ListCell

let cellPresenter = ListCellPresenter(view: mainTabVC)
listCell.inject(presenter: cellPresenter)

window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = mainTabBar
window?.makeKeyAndVisible()

return true
}

VC编写的tableViewDataSource

class MainTabViewController: UIViewController {

private let shared = Sharing.shared

private var presenter: MainTabPresenterInput!
private var userDefaultManager: UserDefaultManager!
func inject(presenter: MainTabPresenterInput, userDefaultManager: UserDefaultManager) {
self.presenter = presenter
self.userDefaultManager = userDefaultManager
}

override func viewDidLoad() {
super.viewDidLoad()
tableViewSetup()

}

func tableViewSetup(){
tableView.delegate = self
tableView.dataSource = self
tableView.register(UINib(nibName: "ListCell", bundle: nil), forCellReuseIdentifier: "ListCell")
}
}

extension MainTabViewController: UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return shared.items.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ListCell") as! ListCell
if let item = presenter.item(row: indexPath.row) {
cell.configure(item: item)
}
return cell
}
}


自定义TableViewCell

class ListCell: UITableViewCell {

private let shared = Sharing.shared
private var presenter: ListCellPresenterInput!

func inject(presenter: ListCellPresenterInput) {
self.presenter = presenter
}

override func awakeFromNib() {
super.awakeFromNib()
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}

@IBAction func favButtonTapped(_ sender: Any) {
let row = self.tag
switch shared.items[row].fav {
case true:
favButton.setImage(UIImage(named: "favIconNon"), for: .normal)
case false:
favButton.setImage(UIImage(named: "favIcon"), for: .normal)
}
presenter.countFavIcon(rowAt: row) // this presenter is nil
}

func configure(item: Item) {
    ・・・
}
}

细胞演示者

protocol AddTabPresenterInput {
func addButtonTapped(item item: Item?)
}

protocol AddTabPresenterOutput: AnyObject {
func clearFields()
func showAlert(alert: UIAlertController)
}

final class AddTabPresenter: AddTabPresenterInput {

private weak var view: AddTabPresenterOutput!
private weak var mainView: MainTabPresenterOutput!
private weak var alertHandller: AlertHandllerProtocol!

let shared = Sharing.shared

init(view: AddTabPresenterOutput, mainView: MainTabPresenterOutput, alertHandller: AlertHandllerProtocol) {
self.view = view
self.mainView = mainView
self.alertHandller = alertHandller
}

func addButtonTapped(item item: Item?) {
print("called")
mainView.updateView()
}
}

如何解决presenter为零的问题?

希望你们中的一些人能帮助我。

谢谢。

最佳答案

UITableView使用可重用的单元格,你可以在google上找到很多信息,这里是其中一篇文章

https://medium.com/ios-seminar/why-we-use-dequeuereusablecellwithidentifier-ce7fd97cde8e

因此,在 AppDelegate 中创建 UITableViewCell 实例,您什么都不做,它会立即释放

创建演示者

    // somewhere in your MainTabViewController
let cellPresenter = ListCellPresenter(view: self)

要注入(inject)演示者,您应该使用 tableView(_:cellForRowAt:) 方法

    // later in code
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ListCell") as! ListCell
cell.inject(presenter: cellPresenter)

if let item = presenter.item(row: indexPath.row) {
cell.configure(item: item)
}
return cell
}

关于swift - 如何将演示者注入(inject)到自定义 uiTableViewCell 作为依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56944892/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com