pre select/highlight UICollectionViewCell on first load of view

后端 未结 9 1234
醉酒成梦
醉酒成梦 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:53

    For Swift 3.0.1 You can try this:

    self.collectionView.selectItem(at: indexPath, animated: true, scrollPosition: [])
    

    or

    self.collectionView.selectItem(at: indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition(rawValue: 0))
    

    For Objective-C You can try this:

    self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
    

    Note: You should use it in viewDidAppear

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-02-07 05:55

    For swift 3

    Use collectionView.selectItem with-in this overload collectionView(UICollectionView, IndexPath)

    This is my code, in this code I pre selected row with indexPath.row = 0

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = ScenarioCollectionView.dequeueReusableCell(withReuseIdentifier: "ReuseScenarioCollectionViewCell", for: indexPath as IndexPath) as! ScenarioCollectionViewCell
    
        if (indexPath.row == 0){
            collectionView.selectItem(at: indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition.centeredHorizontally)
            cell.layer.borderColor=UIColor.gray.cgColor        
        }else{
            cell.layer.borderColor=UIColor.white.cgColor
        }
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath)
        cell?.layer.borderColor = UIColor.gray.cgColor
            }
    
    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath as IndexPath)
        cell?.layer.borderColor = UIColor.white.cgColor
        collectionView.deselectItem(at: indexPath, animated: true)
    }
    
    0 讨论(0)
  • 2021-02-07 06:00

    For Swift 3:

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        //auto selected 1st item
        let indexPathForFirstRow = IndexPath(row: 0, section: 0)
        self.collectionView?.selectItem(at: indexPathForFirstRow, animated: true, scrollPosition: .top)
    }
    
    0 讨论(0)
  • 2021-02-07 06:00

    swift code for selecting a cell initially

    func selectinitialCell() {
    
        let cell = venueTypeCollectionView.cellForItemAtIndexPath(NSIndexPath(forItem: 0, inSection: 0)) as! SearchTypeCollectionViewCell
        self.indexPathOfSelectedItem = NSIndexPath(forItem: 0, inSection: 0)
        cell.venueType.textColor = UIColor.whiteColor()
        cell.selected = true
    }
    

    but this does not invoke the delegate function such as didSelectItemAtIndexPath, didDeselectItemAtIndexPath.

    0 讨论(0)
  • 2021-02-07 06:03
        [self.collectionView reloadData];
        [self.collectionView layoutIfNeeded]; //important
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
        [self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
        [self collectionView:self.collectionView didSelectItemAtIndexPath:indexPath];
    
    0 讨论(0)
提交回复
热议问题