UICollectionView/UITableView delegate/datasource 函数应该放在哪里 Clean Swift [Uncle Bob 的 Clean Architecture]

Where should UICollectionView/UITableView delegate/datasource functions go in Clean Swift [Uncle Bob's Clean Architecture]

我最近将我的应用程序从 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 
}

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