简体   繁体   English

集合视图不显示单元格 swift 5

[英]Collection view doesn't show cells 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.因此,我在 storyboard 视图 controller 中创建了集合视图。 The problem is that my cells 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查看 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 console: true babel: false --> 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 
    }
}

collection view cell class集合视图单元 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 problem is你的问题是

1- Here you set translation after awakeFromNib is called (called meanwhile dequeueReusableCell ) 1- 在这里你在调用awakeFromNib之后设置translation (同时调用dequeueReusableCell

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

so create a function commonly configure所以创建一个 function 常用configure

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

and call it from cellForRowAt并从cellForRowAt调用它

cell.configure(translations[indexPath.row])

2- Xib cells need to be registered like this 2- Xib 单元格需要像这样注册

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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