pre select/highlight UICollectionViewCell on first load of view

后端 未结 9 1250
醉酒成梦
醉酒成梦 2021-02-07 05:48

Im trying to preselect the first object/UICollectionViewCell in the UICollectionView? I have tried:

self.dateCollectionView.allowsMultipleSelection=NO;

[self.da         


        
9条回答
  •  生来不讨喜
    2021-02-07 05:54

    I solved this by subclassing UICollectionView and selecting needed item in layoutSubviews:

    class InitialSelectionCollectionView: UICollectionView {
    
       var initialSetupPerformed: Bool = false
       var initialSelectedIndexPath: IndexPath!
    
       override func layoutSubviews() {
           super.layoutSubviews()
    
           if !initialSetupPerformed && initialSelectedIndex != nil{
               selectItem(at: initialSelectedIndexPath, animated: false, scrollPosition: .centeredHorizontally)
    
               initialSetupPerformed = true
           }
       }
    }
    

    Then, when you init your custom collection view, just set needed IndexPath to initialSelectedIndexPath

提交回复
热议问题