简体   繁体   中英

Swift cells are created but don't show up in UICollectionView swift 5

I'm creating a collection view with custom collection view cell. So, I've created the collection view in my storyboard view controller. The problem is that my cells are created in cellForItemAt but they are not shown at all, I've also tried using default cells, but they are not shown either, and of course, I've tried all the solutions I found. Thank you for helping in advance!

view controller class related parts

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

collection view cell 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)
    }
}

your awakeFromNib method is probably firing before your set your cell's translation property. Try setting your label text in your translation's setter:

    set {
        _translation = newValue
        wordLabel?.text = _translation?.word
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM