1

I'm trying to select the first Item in CollectionView when the Collectionview loads. I found many solutions for Swift 3 but nothing of that worked for me in Swift4. What I tried(viewDidAppear):

import UIKit
import SDWebImage

class PostsTab_Details: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

@IBOutlet weak var PostsSelectRarityCollectionView: UICollectionView!
@IBOutlet weak var RarityTypeLbl: UILabel!

//Center my Collectionview Horizontal
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

        let totalCellWidth = 100 * (selectedPosts?.rarity.count)!
        let totalSpacingWidth = 10 * (3 - 1)

        let leftInset = (PostsSelectRarityCollectionView.frame.width - CGFloat(totalCellWidth + totalSpacingWidth)) / 2
        let rightInset = leftInset

        return UIEdgeInsetsMake(0, leftInset, 0, rightInset)

}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return (selectedPosts?.rarity.count)!
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell =  PostsSelectRarityCollectionView.dequeueReusableCell(withReuseIdentifier: "PostssTab_DetailCollectionCell", for: indexPath) as! PostsTab_DetailsCollectionViewCell

    cell.PostssTab_DetailCollectionImage.sd_setImage(with: URL(string: (selectedPosts?.PostsImageURL)!))



    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    RarityTypeLbl.text = selectedPosts?.rarity[indexPath.row].rarity

}

 //Tried this also with IndexPath(item: 0, section: 0)
override func viewDidAppear(_ animated: Bool) {
    let selectedIndexPath = IndexPath(row: 0, section: 0)
    PostsSelectRarityCollectionView.selectItem(at: selectedIndexPath, animated: true, scrollPosition: .right)
}



var selectedPosts: Posts?

override func viewDidLoad() {
    PostsSelectRarityCollectionView.delegate = self
    PostsSelectRarityCollectionView.dataSource = self
}
}

But this is not working... I hope someone knows what I'm doing wrong..

Thanks in advance

Update: This worked for me now, thanks to Razib's answer:

override func viewWillAppear(_ animated: Bool) {
    self.collectionView(PostsSelectRarityCollectionView, didSelectItemAt: IndexPath(row: 0, section: 0))
}
Nxt97
  • 151
  • 2
  • 9

3 Answers3

4

Your code seems okay to me. If you like to trigger the didSelectItemAt method, use the following code.

 override func viewDidAppear(_ animated: Bool) {
    self.PostsSelectRarityCollectionView(self.PostsSelectRarityCollectionView, didSelectItemAt: IndexPath(row: 0, section: 0))
}
Razib Mollick
  • 4,617
  • 2
  • 22
  • 20
4

To programmatically select the first Item in CollectionViewCell when the Collectionview loads you should code in viewDidAppear:

override func viewDidAppear(_ animated: Bool) {
    let indexPath:IndexPath = IndexPath(row: 0, section: 0)
    collectionView?.selectItem(at: indexPath, animated: false, scrollPosition: .top)
}
Erick Ribeiro
  • 129
  • 1
  • 4
0

Try this (Swift 5):

    let indexPath:IndexPath = IndexPath(row: 0, section: 0)
    collectionView?.selectItem(at: indexPath, animated: false, scrollPosition: .top)
Claytog
  • 618
  • 8
  • 8