0
votes

I inserted Label into CollectionView cell but when I made outlet this error appeared.

error: The currencyLabel outlet from the ViewController to the UILabel is invalid. Outlets cannot be connected to repeating content

class ViewController: UIViewController {
    
    var currency = Currency.getCurrency()

    @IBOutlet var currencyLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }


}

extension ViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return currency.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "currencyCell", for: indexPath) as! CurrencyCollectionViewCell
        
        let currencyList = currency[indexPath.row]
        currencyLabel.text = "\(currencyList.currency) \n \(currencyList.cash)"
        return cell
    }
    
}

screen Storyboard

2
You have to bind(connect) the outlet within the collection view cell class.Raja Kishan

2 Answers

1
votes

You need to create IBOutlet of your currencyLabel in CurrencyCollectionViewCell class, and use it like

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
   let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "currencyCell", for: indexPath) as! CurrencyCollectionViewCell          
   let currencyList = currency[indexPath.row]
   cell.currencyLabel.text = "\(currencyList.currency) \n \(currencyList.cash)"   
   return cell
}
-1
votes

You have made a simple mistake.

You should make below outlet in CollectionViewCell not in your ViewController.

@IBOutlet var currencyLabel: UILabel!