Reset scroll on UICollectionView

前端 未结 9 1267
太阳男子
太阳男子 2021-01-30 05:25

I have a horizontal UICollectionView which works fine and scrolls. When I tap an item I update my data and call reloadData. This works and the new data

9条回答
  •  旧时难觅i
    2021-01-30 05:34

    In Swift:

    collectionView.setContentOffset(CGPointZero, animated: true)
    

    If you leave the view that houses the collection view, make a change in the 2nd view controller, and need the collection view to update upon returning:

    @IBAction func unwindTo_CollectionViewVC(segue: UIStoryboardSegue) {
        //Automatic table reload upon unwind
        viewDidLoad()
        collectionView.reloadData()
        collectionView.setContentOffset(CGPointZero, animated: true)
    }
    

    In my situation the unwind is great because the viewDidLoad call will update the CoreData context, the reloadData call will make sure the collectionView updates to reflect the new CoreData context, and then the contentOffset will make sure the table sets back to the top.

提交回复
热议问题