从 ViewController 到 UILabel 的 currencyLabel 出口无效。插座不能连接到重复的内容

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

我将 Label 插入到 CollectionView 单元格中,但是当我制作 outlet 时出现此错误。

错误:从 ViewController 到 UILabel 的 currencyLabel 出口无效。插座无法连接到重复内容

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
    }
    
}

你犯了一个简单的错误。

您应该 在 CollectionViewCell 中而不是在您的 ViewController 中创建下方出口。

@IBOutlet var currencyLabel: UILabel!

您需要在 CurrencyCollectionViewCell class 中创建 currencyLabelIBOutlet,并像

一样使用它
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
}