【问题标题】:Reload collection view data from another view class从另一个视图类重新加载集合视图数据
【发布时间】:2015-04-26 14:46:38
【问题描述】:

我在一个视图中有两个容器。顶部有一个集合视图。 当从下面的容器中点击一个按钮时,我想从一个按钮更新我的集合视图。我的按钮还更改了我的集合视图使用的数组的值。

我认为 didSet 可以完成这项工作,但不幸的是没有工作。

顶部:

class TopViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    @IBOutlet weak var favoritesCV: UICollectionView!

    var myFavorites = [] {
        didSet {
            self.favoritesCV.reloadData()
        }
    }


    override func viewDidAppear(animated: Bool) {
        myFavorites = favoritesInstance.favoritesArray
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return myFavorites.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell : FavoritesCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! FavoritesCollectionViewCell

        var myPath = myFavorites[indexPath.row] as! String
        cell.myImage.image = UIImage(named: myPath)
        return cell
    }
 }

底部:

class BottomViewController: UIViewController, UIScrollViewDelegate  {

    @IBAction func addFavorites(sender: AnyObject) {
         favoritesInstance.favoritesArray.append("aaaa.jpg")
    }
}

存储类:

class Favorites {
    var favoritesArray:Array <AnyObject>

    init(favoritesArray:Array <AnyObject>) {
        self.favoritesArray = favoritesArray
    }
}

var favoritesInstance = Favorites(favoritesArray:[])

【问题讨论】:

    标签: ios swift view collections


    【解决方案1】:

    我已经添加了

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadList:", name:"load", object: nil)
    

    在我的集合视图类的 viewdidload 中。 还添加了一个选择器,当通知中心调用它时会重新加载我的数据

    func loadList(notification: NSNotification){
        self.favoritesCV.reloadData()
    }
    

    对于按下按钮的其他类:

    NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
    

    斯威夫特 3:

    NotificationCenter.default.addObserver(self, selector: #selector(loadList), name:NSNotification.Name(rawValue: "load"), object: nil)
    
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
    

    【讨论】:

      【解决方案2】:

      斯威夫特 4:

      一级:

      NotificationCenter.default.post(name: NSNotification.Name("load"), object: nil)
      

      带有 collectionView 的类:

      在 viewDidLoad() 中:

      NotificationCenter.default.addObserver(self, selector: #selector(loadList(notification:)), name: NSNotification.Name(rawValue: "load"), object: nil)
      

      和功能:

      @objc func loadList(notification: NSNotification) {
        self.collectionView.reloadData()
      }
      

      【讨论】:

        【解决方案3】:

        太棒了!我在找这个。在 Swift 3 中,代码略有不同。在collectionviewcontroller中:

        NotificationCenter.default.addObserver(self, selector: #selector(RoundCollectionViewController.load), name:NSNotification.Name(rawValue: "reload"), object: nil)
        

        在另一个中:

        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-03-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-10-01
          • 2018-07-23
          相关资源
          最近更新 更多