簡體   English   中英

集合視圖不顯示單元格 swift 5

[英]Collection view doesn't show cells swift 5

我正在使用自定義集合視圖單元格創建集合視圖。 因此,我在 storyboard 視圖 controller 中創建了集合視圖。 問題是我的單元格根本沒有顯示,我也嘗試過使用默認單元格,但它們也沒有顯示,當然,我已經嘗試了我找到的所有解決方案。 感謝您提前提供幫助!

查看 controller class 相關零件

   override func viewDidLoad() {
        super.viewDidLoad()
        
        collectionView.delegate = self
        collectionView.dataSource = self
        
        title = set?.name
        
        
        
        collectionView!.register(WordCollectionViewCell.self, forCellWithReuseIdentifier: Identifies.WordCollectionViewCellIdentifier)
        
        
        let buttonItem  = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addButtonPressed))
        
        
        navigationItem.rightBarButtonItem = buttonItem
        updateTranslations()
    }

hide: false 控制台: true babel: false -->

extension SingleSetViewController : UICollectionViewDelegate, UICollectionViewDataSource  {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print(translations.count)
        return translations.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Identifies.WordCollectionViewCellIdentifier, for: indexPath) as! WordCollectionViewCell
        
    
        cell.translation = translations[indexPath.row]
    
        return cell
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1 
    }
}

集合視圖單元 class

class WordCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var wordLabel: UILabel!
    
    private var showsWord = true
    
    
    private var _translation : Translation?
    var translation : Translation  {
        set {
            _translation = newValue
        }
        get {
            return _translation ?? Translation(word: "", translation: "")
        }
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        
        let gestureRecognizer = UITapGestureRecognizer(target: self , action: #selector(flip))
        gestureRecognizer.numberOfTapsRequired = 1
        self.addGestureRecognizer(gestureRecognizer)
        
        wordLabel.text = _translation?.word
    }
    
    @objc private func flip(_ sender: UIGestureRecognizer){
        let options : UIView.AnimationOptions = [.transitionFlipFromRight]
        UIView.transition(with: self, duration: 0.5, options: options){
            if let translation = self._translation {
                    self.wordLabel.text = self.showsWord ? translation.translation : translation.word
                    self.showsWord = !self.showsWord
            }
        }
    }
    
    static func nib() -> UINib {
        return UINib(nibName: NibNames.WordCollectionViewCellNibName, bundle: nil)
    }
}

你的問題是

1- 在這里你在調用awakeFromNib之后設置translation (同時調用dequeueReusableCell

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Identifies.WordCollectionViewCellIdentifier, for: indexPath) as! WordCollectionViewCell
cell.translation = translations[indexPath.row]

所以創建一個 function 常用configure

func configure(_ translation:Translation) { 
    self.translation = translation
    wordLabel.text = _translation?.word
 }

並從cellForRowAt調用它

cell.configure(translations[indexPath.row])

2- Xib 單元格需要像這樣注冊

collectionView!.register(WordCollectionViewCell.nib(), forCellWithReuseIdentifier: Identifies.WordCollectionViewCellIdentifier)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM