【问题标题】:Swift: Reload collectionView from TabViewSwift:从 TabView 重新加载 collectionView
【发布时间】:2017-06-30 18:34:02
【问题描述】:

我正在构建一个 tabView,其中一个 collectionView 作为选项卡之一。在 tabView 中,我有一个按钮,用于拍摄照片并将其添加到集合视图中。 我面临的问题是从 TabView 重新加载集合视图,因为我收到以下错误: “由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'UICollectionView 必须使用非零布局参数初始化'”

在 TabView 中我调用了这个函数: PhotoListController().reloadData()

而在 PhotoListController 中,那个函数是:

func reloadData() {
    getData() //function that goes through an array with the data of the images
    self.collectionView!.reloadData()
}

当我把所有东西都放在同一个班级时,它工作得很好,但现在我无法解决这个问题:(

如果有人能帮我一把,将不胜感激。

【问题讨论】:

  • 请分享您的完整代码?

标签: ios swift collectionview tabview


【解决方案1】:

好吧,我终于在这个主题中找到了解决方案: How to reload data in a TableView from a different ViewController in Swift

还有这个:

Swift: Reload collection view data from another view class with swift

解决方案与使用通知中心有关

【讨论】:

    【解决方案2】:

    在此代码中: PhotoListController().reloadData() 您创建了 PhotoListController 类的新副本,但您需要应用到已创建的 PhotoListController 类的同一实例。我猜你需要应用委托模式。

    protocol PhotoListDelegate: AnyObject {
        func reloadData()
    }
    
    class PhotoListController : UIViewController, PhotoListDelegate {
        var tableView = TableView()
        var collectionView = UICollectionView()
        
        override func viewDidLoad() {
            tableView.photoListDelegate = self
        }
        func reloadData() {
            getData()
            self.collectionView.reloadData()
        }
        
        func getData() {
            // your code to get data
        }
    }
    
    class TableView: UITableView {
        weak var photoListDelegate : PhotoListDelegate?
        
        
        // then in TableView Class:
        
        func photoCatched() {
            self.photoListDelegate?.reloadData()
        }
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 2020-10-18
      • 2015-08-14
      相关资源
      最近更新 更多