Skip to content
This repository has been archived by the owner on Nov 15, 2020. It is now read-only.

Suggestion: Propagating actions from cell #25

Open
wow-such-amazing opened this issue Nov 13, 2018 · 3 comments
Open

Suggestion: Propagating actions from cell #25

wow-such-amazing opened this issue Nov 13, 2018 · 3 comments

Comments

@wow-such-amazing
Copy link

Hey! :)
It would be nice to have some default mechanism for propagating actions from cell to view controller. One approach that I use is this. Maybe it could be a start point

protocol MyCellDelegate: class {
    fun didTapButton(inside cell: UITableViewCell)
}

final class MyClass: UITableViewCell {
    weak var delegate: MyCellDelegate?

    // This way I need first pass this delegate into adapter initialiser
    func configure(with model: MyCellModel, delegate: MyCellDelegate) {
        self.delegate = delegate
    }

    @IBAction func didTapButton(_ sender: Any) {
        guard let model = model else { return }
        delegate?.didTapButton(inside: self)
    }
}

final class MyViewController: UIViewController, MyCellDelegate {
    func didTapButton(inside cell: UITableViewCell) {
        guard 
            let indexPath = tableView.indexPath(for: cell),
            let model = director.section(at: indexPath.section)?.models[indexPath.item]
        else { return }
        // do some action with model
    }
}
@wow-such-amazing
Copy link
Author

Also there is a library that simplifies work with UITableView. But it doesn't provide mechanism to work with UICollectionView. What it has is CustomActions for cells.
It uses NotificationCenter to invoke actions:
https://github.com/maxsokolov/TableKit/blob/master/Sources/TableCellAction.swift

@wow-such-amazing
Copy link
Author

wow-such-amazing commented Apr 16, 2019

Or maybe UIResponder chain could be used for this purpose. What do you think about these approaches? I can make a PR once we decide on a way how to do that :)

@malcommac
Copy link
Owner

Hi,
I'll be more than happy to look at PR. It could be a nice addition.
However I've moved the framework to a new repository; the official name is Owl: https://github.com/malcommac/Owl.
It introduces several changes and a definitive APIs set. If you want we can continue in it.
Thanks again

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants