我无法在集合视图标题中显示数据

问题描述 投票:0回答:0

我目前正在开发一个类似于 IMDb 的简单应用程序,我的目标是在标头中显示电影海报和标题,这些都是从 API 中检索到的。虽然我成功地在屏幕底部显示了这些信息,但我在标题中显示相同的数据时遇到了困难。标题中没有任何内容

首页标题

import UIKit

class HomeHeader: UICollectionReusableView,ReuseProtocol,NibProtocol {
    
    @IBOutlet weak var collection: UICollectionView!
    @IBOutlet weak var topView: UIView!
    @IBOutlet weak var viewBottom: UIView!
    var items = [MovieResult]()
    
    override func layoutSubviews() {
        collection.registerCell(type: HorizontalCollectionViewCell.self)
    }

    func configure(data: [MovieResult]){
        items = data
        collection.reloadData()
    }
  
}

extension HomeHeader: UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        items.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: HorizontalCollectionViewCell = collectionView.dequeueCell(for: indexPath)
        cell.configure(data: items[indexPath.item])
            return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = collectionView.frame.width * 143 / 375
        let height = collectionView.frame.height * 283 / 812
        return CGSize(width: width, height: height)
    }
}

ViewCell


import UIKit
import SDWebImage

class HorizontalCollectionViewCell: UICollectionViewCell,ReuseProtocol,NibProtocol {

    @IBOutlet weak var movieImage: UIImageView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var ratingLabel: UILabel!
    
    func configure(data: MovieCellProtocol){
        titleLabel.text = data.titleText
        ratingLabel.text = data.ratingText
        movieImage.sd_setImage(with: URL(string: data.posterImage)!)
        movieImage.layer.cornerRadius = 5
        
    }

}

HomeViewController

import UIKit

class  HomeViewController: UIViewController {
    
    @IBOutlet private weak var collectionView: UICollectionView!
    let viewModel = HomeViewModel()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        collectionSetup()
        viewModelConfiguration()
        
        
    }
    
    
    fileprivate func collectionSetup(){
        collectionView.registerCell(type: VerticalCollectionViewCell.self)
        collectionView.registerSupplementaryView(type: HomeHeader.self, ofKind: UICollectionView.elementKindSectionHeader)
    }
    
    fileprivate func viewModelConfiguration(){
        
        viewModel.getCatagoryItems()
        viewModel.getNowPlaying()
        

        viewModel.errorCallBack = { [weak self] errorMesage in
            print("error: \(errorMesage)")
        }
        viewModel.successCallBack = { [weak self] in
            self?.collectionView.reloadData()
        }
        
    }


}
extension HomeViewController:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//        return viewModel.numberOfItems()
        return viewModel.movieItems.count
    }

    

    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: VerticalCollectionViewCell = collectionView.dequeueCell(for: indexPath)
        cell.configure(data: viewModel.movieItems[indexPath.item])
        return cell
    }

    


    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let header: HomeHeader = collectionView.dequeueSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, for: indexPath)
        header.configure(data: viewModel.nowPlayingItems)
        return header
    }




        
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.width * 327 / 375, height: 120)
    }
    



    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        CGSize(width: collectionView.frame.width, height: 365)
    }
    

    
    
}


ios swift uicollectionview uikit
© www.soinside.com 2019 - 2024. All rights reserved.