Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementation of UITableView delegate and datasource in VIPER

I'm writing an app in VIPER architecture for the first time, and can't understand if the UITableView delegate and datasource methods should go into the View, Presenter or the Interactor? I found in some links that it should be part of View class, but that doesn't seem to be right. And even if it's part of View, how will the data reach there because View should technically not ask for data from the presenter. Presenter should push data itself.

like image 247
danialzahid94 Avatar asked Jan 29 '23 09:01

danialzahid94


2 Answers

The link's you read were correct, the delegate and datasource methods for an UITableView in an app with VIPER architecture should stay in the View. About your conclusion about how the data will reach the view, it's wrong because the View itself should ask for Presenter bring the data, and then the Presenter ask for Interactor load the data from web or a database. If you have any questions about the VIPER architecture I definitely recommend these articles:

Article 1: https://blog.mindorks.com/building-ios-app-with-viper-architecture-8109acc72227

Article 2: https://cheesecakelabs.com/blog/best-practices-viper-architecture/

Article 3: https://cheesecakelabs.com/blog/ios-project-architecture-using-viper/

like image 169
Luiz Fernando Salvaterra Avatar answered Jan 31 '23 23:01

Luiz Fernando Salvaterra


Yes, data source and delegate are the parts of a view layer.

If you do not want your view to ask presenter for data, then you can do it like I describe it. The data source class holds viewModels(dummy objects). You can then communicate via an interface. I mean you might understand better on some example:

protocol SomeViewProtocol {
    func set(withVMS vms: [SomeViewModel])
}

final class SomeVC: SomeViewProtocol {

    let dataSource: SomeDataSource
    let tableView: UITableView

    override func viewDidLoad() {
        tableView.dataSource = dataSource
    }

    func set(withVMS vms: [SomeViewModel]) {
        someDataSource.set(withVMS: vms)
        tableView.reloadData()
    }
}
protocol SomePresenterProtocol {
    ...
}

final class SomePresenter: SomePresenterProtocol {

    fileprivate let view: SomeViewProtocol

    //After view did load
    func initAfterLoad() {
        .
        .
        .

        view.set(withVMS: viewModels)
    }
}

But from my perspective, there is nothing wrong with View asking the presenter for the data.

like image 36
Luzo Avatar answered Feb 01 '23 00:02

Luzo