首页 > 解决方案 > 从 ViewController 到 UILabel 的 currencyLabel 出口无效。插座无法连接到重复内容

问题描述

我将标签插入到 CollectionView 单元格中,但是当我制作出口时,出现了这个错误。

错误:从 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
    }
    
}

屏幕故事板

标签: iosswiftuicollectionviewuilabel

解决方案


您需要在课堂上创建IBOutlet您的currencyLabelCurrencyCollectionViewCell,并像使用它一样使用它

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
}

推荐阅读