NSProxy and delegation

402 Views Asked by At

I,m must add some features for framework which constructing flexible datasource for UICollectionView and UITableView. I have class

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
}
}

I have 2 initializers. First for init with one delegate.

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

    self.tableView = tableView
    self.cellProvider = tableView

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

Second with delegate handler which inherited from NSProxy and consist of a few listeners and , that I can add in delegate of 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
}

I must show example with delegate handler.

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)
}
}

When I run my App, I have this error.Class UniversalDelegateHandler was wrote absolutely correct on objective-C. this

and this

I don't even what to do.Wait your help.

0

There are 0 best solutions below