Scooter
Scooter

Reputation: 4166

Unable to update label in UICollectionView cell (Swift)

My app has a UICollectionViewController in a ContainerView that I am unable to change/update a label in any of the custom cells. My viewDidLoad function loads simply loads an array with 5 instances of a custom object that holds an image and label. This will be used later as my data source.

override func viewDidLoad()
{
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    //self.clearsSelectionOnViewWillAppear = false

    // Register cell classes
    //self.collectionView!.registerClass(MyCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    // Do any additional setup after loading the view.
    self.collectionView!.allowsMultipleSelection = true;

    //UICollectionView setup
    let singleImage = UIImage(named: "IMG_0800.jpg")
    let imageName = "Original"

    for var i=0; i<5; i++
    {
        let vehicleObject = VehicleObject(image: singleImage!, label: imageName)
        arrayOfVehicles.append(vehicleObject)
    }
}

This method seems to work, but partially. When the app runs there are indeed five images displayed, but the label still says "Label" which is the default value from the storyboard prototype, but the default photo from the storyboard is replaced with the one I loaded in the viewDidLoad fucntion.

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
    //Configure the cell
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell

    cell.backgroundColor = UIColor .blackColor()
    cell.selectedBackgroundView = UIView(frame: CGRectMake(0, 0, 300, 300))
    cell.selectedBackgroundView?.backgroundColor = UIColor .redColor()

    cell.imageView.image = arrayOfVehicles[indexPath.row].vehicleImage
    cell.imageLabel = arrayOfVehicles[indexPath.row].vehicleLabel

    return cell
}

Lastly this method has no effect on the UILabel in the cell at all. When it runs the println calls display exactly what I would expect (the updated values), but the label in the collection view cell simply doesn't update. It always reads the same as whatever it is initialized to in the storyboard. The image updates, the label does not. I have run out of internet searching for this!

func renameAllCellLabels()
{   
    var selectedItemsIndexPaths = collectionView?.indexPathsForVisibleItems()
    for var i=0; i < selectedItemsIndexPaths?.count; i++
    {
        let selectedItemIndexPath = selectedItemsIndexPaths?[i] as! NSIndexPath
        let cell = collectionView?.cellForItemAtIndexPath(selectedItemIndexPath) as! MyCollectionViewCell

        let singleImage = UIImage(named: "Shelby.jpg")

        let i = selectedItemIndexPath.row
        println(i)
        println(arrayOfVehicles[i].vehicleLabel.text)
        println(cell.imageLabel.text)

        arrayOfVehicles[i].vehicleLabel.text = "Updated"
        cell.imageLabel.text = "Updated"

        arrayOfVehicles[i].vehicleImage = singleImage
        cell.imageView.image = singleImage

        println(arrayOfVehicles[i].vehicleLabel.text)
        println(cell.imageLabel.text)
        println()
    }
    collectionView?.reloadItemsAtIndexPaths(selectedItemsIndexPaths!)
}

Upvotes: 0

Views: 1725

Answers (1)

Aaron Brager
Aaron Brager

Reputation: 66302

You should update the existing label with information from your data model instead of creating a new label each time:

cell.imageLabel.text = // The text…

Upvotes: 2

Related Questions