Marro
Marro

Reputation: 57

The myLabel outlet from the UICollectionView to the UILabel is invalid. Outlets cannot be connected to repeating content

I coded my first CollectionView but the build fails with this error message:

"Illegal Configuration: The myLabel outlet from the UICollectionView to the UILabel is invalid. Outlets cannot be connected to repeating content."

I read other questions on StackOverflow with the same error, and the solution was to set the content of the UILabel which is in a prototype cell and has an outlet to "CollectionViewCell.swift" from static to dynamic. I couldn't try this because this option doesn't appear. I think it's gone with the newer versions of Xcode.

My code in "CollectionViewCell.swift":

import UIKit

class CollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var myLabel: UILabel!
}

My code in "ViewController.swift":


class LibraryViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }

    @IBOutlet weak var sortCollectionView: UICollectionView!

    func numberOfSections(in sortCollectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ sortCollectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }

    func collectionView(_ sortCollectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let sortCell = sortCollectionView.dequeueReusableCell(withReuseIdentifier: "sortCell", for: indexPath) as! CollectionViewCell
        sortCell.myLabel.text = "hi"
        return sortCell
    }

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

Thank you for every helpful answer :'D

Upvotes: 2

Views: 519

Answers (2)

Abdul Karim Khan
Abdul Karim Khan

Reputation: 4935

Select your UILabel from Storyboard and check connections of it, there might be an old connection of it. Remove that old connection and you are good to go.

enter image description here

Upvotes: 1

Marro
Marro

Reputation: 57

Ok, I fixed it by myself! For everyone who search for the solution: Double check if there are no other Outlets than the CollectionViewCell! In my case the Label had an outlet to another thing :^)

Upvotes: 0

Related Questions