简体   繁体   中英

Reloading UICollectionview from subview

I invoke a full screen subview from one of my uicollectionviewcells. This subview adds new cells to the collectionview. Before I remove this subview itself from its superview (uicollectionview) I call update function from the subview itself in uicollectionviewcontroller as below,

func update() {
   self.uicollectionview?.reloadData()
}

But nothing happens until I kill the app and reopen it. I also used dequeue async. But nothing changes. Any help will be appreciated. Thnx,

Update 1:

I call below method from my subview. After some animations, subview removes itself from the superview and calls update function in superview controller. I also swapped the lines and put removeFromSubview method as the last call. But it did not worked.

    func tapHandler(sender: UITapGestureRecognizer? = nil) {
    if sender?.state == .ended {
        UIView.animate(withDuration: 0.2, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .transitionCurlDown, animations: {
            self.recordButton.frame = CGRect(origin: self.recordButton.center, size: CGSize(width: 0, height: 0))
            self.timerLabel.frame = CGRect(origin: self.timerLabel.center, size: CGSize(width: 0, height: 0))
            self.circularProgressBar.frame = CGRect(origin: self.circularProgressBar.center, size: CGSize(width: 0, height: 0))

        }, completion: { (completed) in
            UIView.animate(withDuration: 0.2, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .transitionCurlDown, animations: {
                self.recorderView.frame = CGRect(origin: self.recorderView.center, size: CGSize(width: 0, height: 0))
                self.blurEffectView.effect = nil
            }, completion: { (animationCompleted) in


                self.removeFromSuperview()

                print("I am finished")
                let hdsc = HomeDatasourceController()
                hdsc.update()


            })
        })
    }
}

the update method in superview controller is as below

    func update() {
    print("update() func called")
    DispatchQueue.main.async {
        self.collectionView?.reloadData()
    }

Also the subview is defined as a weak var in superview controller as

weak var audioPlayerView: AudioPlayerView?
weak var audioRecorderView: AudioRecorderView?

Try dispatching the reload on main thread.

dispatch_async(dispatch_get_main_queue(),{ [weak self] in
    self?.uicollectionview?.reloadData()
})

I solved my own question. I forgot to mention that I was using LBTA components API for my collection view. I just reset my datasource once again in update function after I added new objects to the datasource and now it functions.

    func update() {
    print("update() func called")
    let hds = HomeDataSource()
    self.datasource = hds
    collectionView?.reloadData()
}

but it is strange, I was expecting that reloadData() function would do it for me... Thanks to all who helped me through out...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM