【问题标题】:Swift variables on Collectionviewcontroller resetting after calling a function from outside从外部调用函数后,Collectionviewcontroller 上的 Swift 变量重置
【发布时间】:2020-05-03 17:48:10
【问题描述】:

当我使用CollectionViewHeader 上的按钮调用CollectionCiewController 上的函数时,它会使c.view 上的所有变量为零。我找不到问题出在哪里。

 @IBAction func loadmore(_ sender: Any) {
        CollectionViewController().goNetwork()  
    }

调用函数:

import UIKit

class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
 var query: QueryForUnogs!
 var dataSource = [REsult]() {
        didSet {
            DispatchQueue.main.async {
                self.collectionView.reloadData()
            }
        }
    }
override func viewDidLoad() {
        super.viewDidLoad()
        self.goNetwork()
}

func goNetwork() {
        var urlWithParams: String = "https://unogsng.p.rapidapi.com/search?start_year=\(query.minYear!)&end_year=\(query.maxYear!)&start_rating=\(query.minImdb!)&offset=\(self.offset.description)&type=\(query.type!)&end_rating=10&countrylist=\(query.cc!)&orderby=\(query.orderby!)&audio=\(query.audio!)&subtitle=\(query.subtitle!)"

    NetworkService().downloadUnogs(_qu: urlWithParams) { result in
        switch result {
        case .failure(let error): print(error)
        case .success(let RR):
        self.dataSource = RR.results!
}}}}

【问题讨论】:

  • 您是否在RR.results 中获取数据?
  • 您在哪个控制器中执行此操作? @IBAction func loadmore(_ sender: Any) { CollectionViewController().goNetwork() }
  • 我从页脚调用它。 CollectionReusableView
  • 你能把那个代码和课程贴出来吗?
  • class CollectionReusableView: UICollectionReusableView { @IBOutlet weak var loadBtn: UIButton! @IBAction func loadmore(_ sender: Any) { let cvc = CollectionViewController() as CollectionViewController cvc.goNetwork() }" 我试过了,但是不行

标签: ios swift function collectionview


【解决方案1】:

您正在 CollectionViewController 的新实例上调用函数,这就是为什么所有内容都为零的原因...通过委托获取当前的 CollectionViewController 并在现有的 CollectionViewController 对象上调用 goNetwork

像这样写一个协议

protocol CollectionHeaderViewDelegate {
  func didTapButton()
}

用委托编写CollectionReusableView

class CollectionReusableView: UICollectionReusableView {

  @IBOutlet weak var loadBtn: UIButton!
  var delegate: CollectionHeaderViewDelegate?

  @IBAction func loadmore(_ sender: Any) {
    delegate?.didTapButton()

  }

}

在您的主控制器类中,即CollectionViewController 写这个函数

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    if (kind == UICollectionView.elementKindSectionFooter) {
      let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "LoadFooter", for: indexPath) as! CollectionReusableView
      footerView.delegate = self
      return footerView

    }
    fatalError()

  }

CollectionViewController的扩展名并用协议确认

extension CollectionViewController: CollectionHeaderViewDelegate {
  func didTapButton() {
     goNetwork()
  }


}

现在你的主控制器中有 goNetwork .... 它会自动加载内容 ...

谢谢

【讨论】:

  • 它有效。现在没有重置。感谢您的出色解决方案
猜你喜欢
  • 2013-11-14
  • 1970-01-01
  • 1970-01-01
  • 2016-05-07
  • 2021-09-22
  • 2015-11-20
  • 1970-01-01
  • 2023-04-03
  • 2012-07-14
相关资源
最近更新 更多