简体   繁体   English

在VIPER中实现UITableView委托和数据源

[英]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? 我是第一次在VIPER架构中编写应用程序,并且无法理解UITableView委托和数据源方法是否应该进入View,Presenter或Interactor? I found in some links that it should be part of View class, but that doesn't seem to be right. 我在一些链接中发现它应该是View类的一部分,但这似乎不对。 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. 即使它是View的一部分,数据将如何到达那里,因为View在技术上不应该要求演示者提供数据。 Presenter should push data itself. 演示者应该自己推送数据。

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 . 您阅读的链接是正确的,具有VIPER体系结构的应用程序中UITableView的委托和数据源方法应保留在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. 关于数据如何到达视图的结论,这是错误的,因为View本身应该要求Presenter带来数据,然后Presenter要求Interactor从Web或数据库加载数据。 If you have any questions about the VIPER architecture I definitely recommend these articles: 如果您对VIPER架构有任何疑问,我肯定会推荐这些文章:

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

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

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

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). 数据源类包含viewModels(虚拟对象)。 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. 但是从我的角度来看,View向主持人询问数据并没有错。

It is permittable to leave datasource in View (and is probably the right place, if we don't consider any other layers). 允许在View中保留数据源(如果我们不考虑任何其他层,则可能是正确的位置)。 However, it is not 100% correct from SOLID perspective. 但是,从SOLID的角度来看,它并非100%正确。 VIPER was made up to push single responsibilty principle . VIPER是为了推动单一责任原则 Leaving table datasource/delegate in View may well lead to breaking this principle due to non-view-related code, potentially possible in delegate/datasource. View保留表数据源/委托可能会导致违反此原则,因为非视图相关的代码,可能在委托/数据源中。 It is much better to restrict View to be only responsible for View -related tasks. View限制为负责与View相关的任务要好得多。 It should not, ideally, serve as data provider, even as a datasource for a tableview. 理想情况下,它不应该作为数据提供者,即使作为tableview的数据源也是如此。 That said, the best practice is to implement table view DataSource/Delegate separately from either Presenter and the View. 也就是说,最佳做法是将表视图DataSource / Delegate Presenter和View 分开实现。 Declare datasource(delegate) within View and assign it to your table: View中声明数据源(委托)并将其分配给您的表:

let dataSource: DataSource! // Implements both TableView DataSource and Delegate protocols
let tableView: UITableView!

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

That datasource will then communicate with either the view or presenter (if needed) via output protocols, as it is common in VIPER. 然后,该数据源将通过输出协议与视图或演示者(如果需要)进行通信,这在VIPER中很常见。

The way DataSource would get its data is from Presenter , but not by itself, but rather via View , which obtains it data from Presenter 's output interface. DataSource获取数据的方式来自Presenter ,但不是单独使用,而是通过ViewPresenter的输出接口获取数据。 The latter is sometimes discussable and depends on complexity of your app. 后者有时可以讨论,并取决于您的应用程序的复杂性。 It is possible to bridge Presenter and tableview DataSource with communication protocols and can be implemented well, but that depends on the approach adopted in your team. 可以使用通信协议桥接Presenter和tableview DataSource,并且可以很好地实现,但这取决于团队采用的方法。 VIPER is about driving big projects and its practices must be convenient for entire team involved. VIPER是关于推动大项目的,其实践必须方便整个团队参与。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM