0

我必须为框架添加一些功能,为 UICollectionView 和 UITableView 构建灵活的数据源。我有课

import UIKit

public class SimpleTableController: AbstractController,   UITableViewDelegate, UITableViewDataSource {
private weak var tableView: UITableView?
public weak var tableViewDelegate: UITableViewDelegate?
public override var itemModels: [ItemModel] {
    didSet {
        tableView?.reloadData()
    }
}

public init(tableView: UITableView) {
    super.init()

    self.tableView = tableView
    self.cellProvider = tableView

    self.tableView?.delegate = self
    self.tableView?.dataSource = self
}

public init(tableView: UITableView, withDelegateHandler handler:UniversalDelegateHandler){
    super.init()
    handler.addListener(self)
    self.tableView = tableView
    self.cellProvider = tableView
    self.tableView?.delegate = (handler as! UITableViewDelegate)
    self.tableView?.dataSource = self
}



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

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    return reuseCell(for: Flexy.Index(section: indexPath.section, item: indexPath.row), from: tableView)
}

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    didClickOnItem(on: Flexy.Index(section: indexPath.section, item: indexPath.row))

    tableViewDelegate?.tableView?(tableView, didSelectRowAt: indexPath)
}

public override func responds(to selector: Selector!) -> Bool {
    let haveSelector = super.responds(to: selector)

    if !haveSelector,
        let delegate = tableViewDelegate {
        return delegate.responds(to: selector)
    }
    return haveSelector
}
}

我有 2 个初始化程序。首先是一个代表的初始化。

public init(tableView: UITableView) {
    super.init()

    self.tableView = tableView
    self.cellProvider = tableView

    self.tableView?.delegate = self
    self.tableView?.dataSource = self
}

其次是委托处理程序,它继承自 NSProxy 并由一些侦听器组成,我可以将其添加到 tableView 的委托中。

public init(tableView: UITableView, withDelegateHandler handler:UniversalDelegateHandler){
    super.init()
    handler.addListener(self)
    self.tableView = tableView
    self.cellProvider = tableView
    self.tableView?.delegate = (handler as! UITableViewDelegate)
    self.tableView?.dataSource = self
}

我必须展示委托处理程序的示例。

import UIKit

class ContactListViewController: UIViewController {
@IBOutlet private var tableView: UITableView!

var delegateHandler = UniversalDelegateHandler(for: tableView as? Protocol)
var tableController: SimpleTableController!
override func viewDidLoad() {
    super.viewDidLoad()
    self.delegateHandler?.addListener(self)
    tableController = SimpleTableController(tableView: tableView, withDelegateHandler:delegateHandler!)
    tableController.register(binder: ContactViewBinder({[weak self] item in
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        guard let contactItemViewController = storyboard.instantiateViewController(withIdentifier: "ContactInfoViewController") as? ContactItemViewController else { return }
        contactItemViewController.loadViewIfNeeded()
        contactItemViewController.avatarImage = item.photo
        contactItemViewController.name = item.name

        self?.show(contactItemViewController, sender: nil)
    }))
    tableController.tableViewDelegate = (delegateHandler as! UITableViewDelegate)
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    tableController.itemModels = [
        ContactItemModel(photo: UIImage(named: "avatar1")!, name: "Marie"),
        ContactItemModel(photo: UIImage(named: "avatar3")!, name: "Benjamin"),
        ContactItemModel(photo: UIImage(named: "avatar1")!, name: "Sofia"),
        ContactItemModel(photo: UIImage(named: "avatar2")!, name: "Barry"),
        ContactItemModel(photo: UIImage(named: "avatar1")!, name: "Elizabeth"),
        ContactItemModel(photo: UIImage(named: "avatar1")!, name: "Chloe"),
        ContactItemModel(photo: UIImage(named: "avatar3")!, name: "Brian"),
        ContactItemModel(photo: UIImage(named: "avatar2")!, name: "Corwin"),
        ContactItemModel(photo: UIImage(named: "avatar3")!, name: "Christopher"),
    ]
}
}

extension ContactListViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
}
}

当我运行我的应用程序时,我有这个错误。Class UniversalDelegateHandler 在objective-C 上写的绝对正确。 这个

和这个

我什至不知道该怎么做。等你的帮助。

4

0 回答 0