简体   繁体   中英

Show specific cells in collection view. Swift

I have 10 pictures that I show in collection view. There are also two buttons that should sort this one collection view. When I launch the application 1-5 cells and other hidden ones should be shown. when I click on the second button, these cells should be hidden and another will appear. How can I implement this?

ViewController

let practice = true

@IBAction func theoryButtonAction(_ sender: UIButton) {
   
    practice = false
    collectionView.reloadData()
}
@IBAction func practiceButtonAction(_ sender: UIButton) {

    practice = true
    collectionView.reloadData()
}
    
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: GameCollectionViewCell.identifier, for: indexPath) as? GameCollectionViewCell else {
        return UICollectionViewCell()
    }
     
    let game = gamesList[indexPath.row]

    cell.gameLabel.text = game.name.localized
    cell.gameLabel.roundCorners(.allCorners, radius:cell.gameLabel.frame.height / 2 )
    cell.contentView.roundCorners(.allCorners, radius: 50.0)
    
    /*
        if practice = true. Hide cell 1-5 and show 5-10
        if practice = false Hide cell 5-10 and show 1-5
    */
  
    return cell
}

Your collection view has a backing store of data (generally an array). This backing store does not have to always be the full backing store. Simply create other backing stores which contain the data you want to display at this particular moment.

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