程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在 VIPER 中构建 UITableView大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决在 VIPER 中构建 UITableView?

开发过程中遇到在 VIPER 中构建 UITableView的问题如何解决?下面主要结合日常开发的经验,给出你关于在 VIPER 中构建 UITableView的解决方法建议,希望对你解决在 VIPER 中构建 UITableView有所启发或帮助;

想象一下我有一个像这样的 vIEwForheaderInSection

注意:目前是用 MVP 编写的,我正在尝试将其重构为 VIPER

func tableVIEw(_ tableVIEw: UItableVIEw,vIEwForheaderInSection section: int) -> UIVIEw? {
        
    guard let header = tableVIEw.dequeueReusableheaderfooterVIEw(withIDentifIEr: DetailsSectionheaderVIEw.IDentifIEr) as? DetailsSectionheaderVIEw,let section = PresenterEnum.Sections(rawValue: section),section != .fourth,let presenter = presenter else { 
        return nil 
    }
        
    switch section {
        case .first:
            header.configure(StringConstants.first.localized,showbutton: false,isSELEcted: falsE)
        case .second:
            let secondarray = presenter.secondarrayCount
            header.configure(StringConstants.second.localized,showbutton: secondarray > 3,isSELEcted: presenter.isExpanded(section.rawvalue))
            header.showMoreAction = { [weak self] in
                self?.handleMoreAction(tableVIEw,in: section.rawvalue)
            }
        case .third:
            let thirdCount = presenter.thirdArrayCount
            header.configure(StringConstants.third.localized,showbutton: thirdArrayCount > 3,isSELEcted: presenter.isExpanded(section.third))
            header.showMoreAction = { [weak self] in
                self?.handleMoreAction(tableVIEw,in: section.rawvalue)
            }
        default:
            return nil
        }
        
    return header
}

private func handleMoreAction(_ tableVIEw : UItableVIEw,in section : int){
    guard let presenter = presenter else {return}
        
    tableVIEw.beginupdates()
    let isExpanded = presenter.isExpanded(section)
        
    var indexPaths = [IndexPath]()
        
    for i in presenter.minimumRowCount..<presenter.@R_254_10586@lRowCountForSection(section){
        indexPaths.append([section,i])
    }
        
    if isExpanded{
       tableVIEw.deleteRows(at: indexPaths,with: .automatiC)
    }else{
       tableVIEw.insertRows(at: indexPaths,with: .automatiC)
    }
        
    presenter.updateExpansion(section)
    tableVIEw.endupdates()
}

UItableVIEwDelegateUItableVIEwDatasourceVIEw 的一部分。但它们是被动的,不应真正向 presenter 请求信息。但同时如何显示视图信息属于presenter

根据上面的定义,我无法弄清楚如何重构下面的代码。这似乎不正确,因为关于该部分应该显示什么的逻辑在视图中以及如何处理 showMoreAction

所以我的问题是: 构建上述两种方法以使其更符合 VIPER 标准的“正确”方法是什么?

解决方法

1 创建允许视图和演示者传递数据的协议, 示例:

protocol PresenterProtocol where Self: UIViewController{
   var table: UITableView {get}
}
  1. 在Presenter中创建弱视图控制器引用,符合Presenter协议, 例子:

    class Presenter: NSObject {
    
      weak var viewController: PresenterProtocol{
         didSet{
           connectVC()
         }
      }
    
      func connectVC(){
         self.viewController.table.delegate = self
         self.viewController.table.datasource = self
      }
    }
    
  2. 现在,当您将 tableview 委托和数据源分配给演示者时,您可以进行扩展,例如:

     extension Presenter: UITableViewDatasource,...
     // cell for row methods .. 
    
  3. 那么你应该改变你的视图(ViewController)

     class ViewController: UIViewController{
         @IBOutlet weak var tableView: UITableView?
         var presenter: Presenter?
    
    
         viewDidLoad(){
            super.viewDidLoad()
            presenter = Presenter()
            presenter.viewController = self
         }
     }
    

所以你将你的 vc 分配给了演示者的 viewController 现在你需要的只是让你的 VC 符合 PresenterProtocol

      extension ViewController: PresenterProtocol{
        var table: UITableView {
            return self.tableView
        }
      }

现在你的 VC 是干净的,所有的逻辑都包含在演示者中,当然可以更容易,但我只是展示了正确的方向

大佬总结

以上是大佬教程为你收集整理的在 VIPER 中构建 UITableView全部内容,希望文章能够帮你解决在 VIPER 中构建 UITableView所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:中构建