How to access text of a label in a UICollectionViewCell from a different method?

1k views Asked by At

I have a class of type UICollectionViewController. In this class I have variable for the label of a UICollectionViewCell:

var cellTitle = UILabel()

I also have the collectionView method cellForItemAtIndexPath:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell
    var definitions = shuffle(["Used to carry the pharoah","Used to carry bodies as a ceremony","Had a flat deck to carry a farmer's treasure","Daily, it made a trip around the world to carry Ra","Towed by smaller boats, carrying heavy objects","Used for business and pleasure by officials/nobles","Carried most Egyptians and some goods"])
    var boatTypes = shuffle(["Ferry","Funeral Barge","Cargo Boat","Cattle Boat","Royal Boat","The Sun Boat","Grand Boat"])
    // Configure the cell
    cellTitle = UILabel(frame: CGRectMake(0, 0, cell.bounds.size.width, 160))
    cell.contentView.addSubview(cellTitle)
switch indexPath.item {
    case 0:
        cellTitle.text = definitions[0]
    case 1:
        cellTitle.text = definitions[1]
    case 2:
        cellTitle.text = definitions[2]
    case 3:
        cellTitle.text = definitions[3]
    case 4:
        cellTitle.text = definitions[4]
    case 5:
        cellTitle.text = definitions[5]
    case 6:
        cellTitle.text = definitions[6]
    case 7:
        cellTitle.text = boatTypes[0]
    case 8:
        cellTitle.text = boatTypes[1]
    case 9:
        cellTitle.text = boatTypes[2]
    case 10:
        cellTitle.text = boatTypes[3]
    case 11:
        cellTitle.text = boatTypes[4]
    case 12:
        cellTitle.text = boatTypes[5]
    case 13:
        cellTitle.text = boatTypes[6]
    default:
        break

I also have a didSelectItemAtIndexPath method:

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    var selectedText = cellTitle.text
    switch indexPath.item {
    case 0:
        if selectedText == "Used to carry the pharoah" {
            println("Used to carry the pharoah")
        } else {
            println("Not Working")
        }

The problem is that when I press the cell that the label does have the text "Used to carry the pharoah", println("Not Working") is triggered. How can I pass the value from the cellForItemAtIndexPath method to the didSelectItemAtIndexPath method so the value of cellTitle stays the same? Thank you.

1

There are 1 answers

1
neelamc23 On BEST ANSWER

cellForItemAtIndexPath is called for each item when it is to be displayed. You are applying some sort of a "shuffle" on the array of string? So, not all strings will necessarily be used and a few will be have same text. You can simply have the variables 'definitions' and 'boats' globally initialized and then access the value like this:

if indexPath.item < 7 {
    selectedText = definitions[indexPath.item]
} else {
    selectedText = boatTypes[indexPath.item]
}