iosswiftuitableviewviper-architecture

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.


Solution

  • 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.