Richard
Richard

Reputation: 57

How to Get Label Text from Selected Collection View Cell with CoreData?

I am trying to get the text of a label that is in a collection view cell when it is pressed. I know that doing it the regular way would involve using [indexPath.row] function however the array that the collection view is made from is with CoreData. When I try and use the [indexPath.row] it says: "'subscript' is unavailable: cannot subscript String with an Int, see the documentation comment for discussion." This is how my didSelect function currently looks like:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellP", for: indexPath) as! CCCollectionViewCell
    id = cell.pLabel.text![Project.name]

    print(id)
}

I am trying to save the text in the label of the collection view cell selected to the variable 'id'. This is the declaration of the collection view:

var projectList : [Project] = []

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

    let project = projectList[indexPath.row]

    cell.pLabel?.text = project.name!

    //cell.tag = indexPath.row

    return cell
}

Note: Project is a CoreData entity and name is the attribute.

Does anyone know how I can save the text when cell is clicked to the 'id' variable?

Upvotes: 0

Views: 4022

Answers (2)

Ashley Mills
Ashley Mills

Reputation: 53111

Ideally, you shouldn't be exposing IBOutlets in your cell. Instead…

class CCCollectionViewCell: UICollectionViewCell {

    IBOutlet weak var pLabel: UILabel!

    var project: Project? {
        didSet {
            pLabel.text = project?.name       
        }
    }
}

and then in your view controller…

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellP", for: indexPath) as! CCCollectionViewCell
    cell.project = projectList[indexPath.row]
    return cell
}

and…

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    cell = collectionView.cellForRow(atIndexPath: indexPath) as! CCCollectionViewCell
    let project = cell.project

    print(project)
}

or…

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let project = projectList[indexPath.row]

    print(project)
}

Upvotes: 1

dvdblk
dvdblk

Reputation: 2935

You should not be dequeuing a new collectionViewCell like this inside didSelectItemAt. The func you are looking for is collectionView.cellForItem(at: indexPath) which returns the cell which was selected.

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    guard let cell = collectionView.cellForItem(at: indexPath) as? CCCollectionViewCell else {
        // couldn't get the cell for some reason
        return 
    }

    id = cell.pLabel.text![Project.name] // ?

    print(id)
}

I am not sure what you are trying to do here. You stated that you want to save the cell's label.text into your id variable. Why are you trying to subscript the text with [Project.name]?

Upvotes: 2

Related Questions