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

Suggestion: Propagating actions from cell #25

@wow-such-amazing

Description

@wow-such-amazing

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions