大佬教程收集整理的这篇文章主要介绍了在 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()
}
UItableVIEwDelegate
和 UItableVIEwDatasource
是 VIEw
的一部分。但它们是被动的,不应真正向 presenter
请求信息。但同时如何显示视图信息属于presenter
。
根据上面的定义,我无法弄清楚如何重构下面的代码。这似乎不正确,因为关于该部分应该显示什么的逻辑在视图中以及如何处理 showMoreAction
。
所以我的问题是: 构建上述两种方法以使其更符合 VIPER 标准的“正确”方法是什么?
1 创建允许视图和演示者传递数据的协议, 示例:
protocol PresenterProtocol where Self: UIViewController{
var table: UITableView {get}
}
在Presenter中创建弱视图控制器引用,符合Presenter协议, 例子:
class Presenter: NSObject {
weak var viewController: PresenterProtocol{
didSet{
connectVC()
}
}
func connectVC(){
self.viewController.table.delegate = self
self.viewController.table.datasource = self
}
}
现在,当您将 tableview 委托和数据源分配给演示者时,您可以进行扩展,例如:
extension Presenter: UITableViewDatasource,...
// cell for row methods ..
那么你应该改变你的视图(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
}
}
以上是大佬教程为你收集整理的在 VIPER 中构建 UITableView全部内容,希望文章能够帮你解决在 VIPER 中构建 UITableView所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。