【问题标题】:Where should UICollectionView/UITableView delegate/datasource functions go in Clean Swift [Uncle Bob's Clean Architecture]UICollectionView/UITableView 委托/数据源函数在 Clean Swift 中应该放在哪里 [Uncle Bob 的干净架构]
【发布时间】:2020-05-16 10:11:59
【问题描述】:

我最近将我的应用程序从 MVC 切换到 Clean Swift,但我无法决定将某些内容放在哪些类中。例如,UICollectionView/UITableViewdelegate/datasource 函数应该放在 Interactor 还是 Presenter 中?或者可能某些函数,比如didSelectItemAt应该放在Interactor中,因为它们处理输入,而其他函数,比如cellForItemAt,应该放在Presenter中,因为它们处理视图。

在决定将某些功能放在Clean Swift 中的哪个位置时,您的决策过程是什么?`

目前我的ViewController中有以下内容

{
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool)
    func scrollViewDidScroll(_ scrollView: UIScrollView)

}

以下在我的Interactor

{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
}

以及我的Presenter中的以下内容

{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
}

【问题讨论】:

    标签: ios swift design-patterns architecture clean-architecture


    【解决方案1】:

    一般来说,Apple 的带有UI 前缀的内置协议应该保留在视图控制器中。如果它变得太复杂,您可以简单地将数据源和委托方法移动到扩展。它们很少变得复杂到足以保证拥有自己的控制器。如果是这样,则意味着您可能有一些业务逻辑应该被提取并移动到交互器。但您仍应将这些方法留在视图控制器或其扩展中。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    • 2016-02-29
    相关资源
    最近更新 更多