为什么不去CollectionViewCell?

时间:2020-10-18 13:29:34

标签: ios swift uicollectionview uicollectionviewcell mobile-development

我注意到调试时Topics CollectionViewCell没有消失。为什么会这样? 所有单元格都正确,但是collectionview不可用。

该项目的链接在下面。您也可以从那里看。

这里我定义单元格。

  extension ArticlesVC: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return models.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: CollectionTableViewCell.identifier, for: indexPath) as! CollectionTableViewCell
        cell.configure(with: models)
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 200
    }
}

struct Model {
    let text: String
    let imageName: String
    
    init(text: String, imageName: String) {
        self.text = text
        self.imageName = imageName
    }
}

在这里我定义CollectionTableViewCell

 class CollectionTableViewCell: UITableViewCell {
    
    static let identifier = "CollectionTableViewCell"
    
    var collectionView: UICollectionView?
    
    var models = [Model]()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        collectionView?.register(TopicsCollectionViewCell.self, forCellWithReuseIdentifier: TopicsCollectionViewCell.identifier)
        
        collectionView?.delegate = self
        collectionView?.dataSource = self
        
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        contentView.addSubview(collectionView!)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        collectionView?.frame = contentView.bounds
    }
    func configure(with models: [Model]) {
        self.models = models
        collectionView?.reloadData()
        
    }
    
}
extension CollectionTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return models.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TopicsCollectionViewCell.identifier, for: indexPath) as! TopicsCollectionViewCell
        cell.configure(with: models[indexPath.row])
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 250, height: 250)
    }
    
}

**此TopicsCollectionViewCell **

    class TopicsCollectionViewCell: UICollectionViewCell {
    
    static let identifier = "TopicsCollectionViewCell"
    
    let titleLabel: UILabel = {
        let label = UILabel()
        label.text = "label"
        return label
    }()
    
    let photoImage: UIImageView = {
       let imageView = UIImageView()
        return imageView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(titleLabel)
        contentView.addSubview(photoImage)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        photoImage.frame = CGRect(x: 3,
                                  y: 3,
                                  width: contentView.width - 6,
                                  height: 150)
        titleLabel.frame = CGRect(x: 3,
                                  y: photoImage.bottom + 5,
                                  width: contentView.width - 6,
                                  height: contentView.height - photoImage.height - 6)
    }
    
    public func configure(with model: Model) {
        print(model)
        self.titleLabel.text = model.text
        self.photoImage.image = UIImage(named: model.imageName)
    }
}

This link is project with github

1 个答案:

答案 0 :(得分:1)

在执行期间,这些行无用,因为collectionView?为零

collectionView?.register(TopicsCollectionViewCell.self, forCellWithReuseIdentifier: TopicsCollectionViewCell.identifier) 
collectionView?.delegate = self
collectionView?.dataSource = self

您需要通过首先初始化collectionView来重新排序代码

let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
contentView.addSubview(collectionView!)
collectionView?.register(TopicsCollectionViewCell.self, forCellWithReuseIdentifier: TopicsCollectionViewCell.identifier) 
collectionView?.delegate = self
collectionView?.dataSource = self
    
相关问题