Allow CollectionView cells to have different identifiers each time

Asked 2 years ago, Updated 2 years ago, 75 views

Whenever cellForItemAtIndexPath is called in the CollectionView cell, I want to have a different identifier every time, but I don't know how to do it.Or is it not possible?
For example, on the first screen read, you generate 30 reusable cells with one identifier, and the next time the screen is loaded, you generate 30 reusable cells with another identifier.

CalendarCell*cell=[collectionView requestReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

The error must register anibor a class for the identifier or connect a protocol cell in a storyboard' appears on the line above in the cellForItemAtIndexPath(identifier in the above line contains a different string each time the screen is loaded.) However, cellForItemIndexPatate>in Storyboard Collection Reusable View can contain only one previously specified string.
So how can I generate a cell with a different identifier each time?
Please let me know if anyone knows.I'm sorry, but I appreciate your cooperation.

ios xcode uicollectionview uicollectionviewcell

2022-09-30 18:56

1 Answers

You can create multiple Cell Identifiers by changing the Items value in the Storyboard and Attributes Inspector in the Collection View as shown below.If you set the item to 2, you can see that there are two cell prototypes in the Collection View.

Enter a description of the image here

"It ended without meeting the questioner's intentions, but I think it would be a waste to delete my answer as it is, so I would like to complete the answer by posting a sample of the """" program where cells change every time I tap the """" cell."

On the Storyboard, create seven cell prototypes, each with Identifier, CellA, CellB, ~, CellF, and CellG.

Enter a description of the image here

Edit the ViewController code.

import UIKit

classViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate{
    // An identifier of a cell is made constant by an array.
    let identifiers: [String] = ["CellA", "CellB", "CellC", "CellD", "CellE", "CellF", "CellG" ]
    // An integer variable that indicates the number of the array identifiers.
    var num —Int=0

    override func viewDidLoad(){
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from anib.
    }

    override funcdidReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // Collection View Datasource and Delete
    func numberOfSectionsInCollectionView (collectionView:UICollectionView) - > Int {
        return1
    }

    functioncollectionView (collectionView:UICollectionView, numberOfItemsInSection section:Int) - > Int {
        return 30 // The number of cells is fixed at 30
    }

    functioncollectionView (collectionView:UICollectionView, cellForItemAtIndexPathindexPath:NSIndexPath) - >UICollectionViewCell{
        // Place the numth Identifier cell.
        letcell=collectionView.dueReusableCellWithReuseIdentifier (identifiers [num], forIndexPath:indexPath) as!UICollectionViewCell
        return cell
    }

    functioncollectionView(collectionView:UICollectionView, didSelectItemAtIndexPathindexPath:NSIndexPath){
        // Each time a cell is tapped, it switches to CellA→CellB→ through→CellF→CellG→CellA→CellB→ through.
        num=(num+1)%7
        collectionView.reloadData()
    }

}

Enter a description of the image here


2022-09-30 18:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.