Как добавить нижний колонтитул / верхний колонтитул в CollectionView - PullRequest
0 голосов
/ 31 мая 2019

Фрагмент кода

class TemporaryViewController: UIViewController {
     @IBOutlet weak var itemCollectionView: UICollectionView!   // Main Collection View
     private var footerView: DashboardBottomViewCollectionViewCell?

     override func viewDidLoad() {
       super.viewDidLoad()
        self.itemCollectionView.delegate = self
        self.itemCollectionView.dataSource = self
        self.itemCollectionView.register(UINib(nibName: nibParameter.DASHBOARD_CENTER_NIB, bundle: nil), forCellWithReuseIdentifier: nibParameter.DASHBOARD_CENTER_NIB)
        self.itemCollectionView.register(UINib(nibName: "DashboardBottomViewCollectionViewCell", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "DashboardBottomViewCollectionViewCell")
       }}

extension TemporaryViewController:  UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

     func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

       switch kind {
          case UICollectionView.elementKindSectionHeader:
          return nil

          case UICollectionView.elementKindSectionFooter:
           let footerView = itemCollectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "DashboardBottomViewCollectionViewCell", for: indexPath) as! DashboardBottomViewCollectionViewCell
           self.footerView = footerView
           self.footerView?.contentView?.clipsToBounds = true
           footerView?.dashboardBottomDelegate = self
           footerView?.clipsToBounds = true
           return footerView!

    default:
        return headerView!
    }
}

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
     return CGSize(width: 600, height: 300.0)
}

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
     return CGSize(width: 100.0, height: 300.0)
}


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
     return 3
}

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

     let width = -10 + (collectionView.frame.width)
     return CGSize(width: width,
                  height: 90)
}      
}

Класс нижнего колонтитула для отображения коллекции Просмотр

class DashboardBottomViewCollectionViewCell: UICollectionReusableView {

weak var dashboardBottomDelegate: DashboardBottomViewCollectionViewDelegate?
@IBOutlet weak var itemCollectionView: UICollectionView!
@IBOutlet weak var imageView: UIView!
var estimateWidth = 160.0
var cellMarginSize = 16.0
var contentView : DashboardBottomViewCollectionViewCell?

override init(frame: CGRect) {
    super.init(frame: frame)
    let contents = Bundle.main.loadNibNamed("DashboardBottomViewCollectionViewCell", owner: self, options: nil)?.first as! DashboardBottomViewCollectionViewCell
    self.addSubview(contents)
    contentView = contents
    contents.itemCollectionView.register(UINib(nibName: nibParameter.DASHBOARD_PURCHASE_NIB, bundle: nil), forCellWithReuseIdentifier:nibParameter.DASHBOARD_PURCHASE_NIB)
    contents.itemCollectionView.delegate = self
    contents.itemCollectionView.dataSource = self
    let flow = contents.itemCollectionView?.collectionViewLayout as! UICollectionViewFlowLayout
    flow.minimumInteritemSpacing = CGFloat(self.cellMarginSize)
    flow.minimumLineSpacing =  CGFloat(self.cellMarginSize)
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

func reloadData() {
    contentView?.itemCollectionView.reloadData()
}
}

extension DashboardBottomViewCollectionViewCell: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 15
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: nibParameter.DASHBOARD_PURCHASE_NIB, for: indexPath) as! RecentPurchaseCollectionViewCell

    cell.purchaseLabel.text = "trainers.trainerName"
    cell.imageView.sd_setImage(with: URL(string: "trainers.tarinerProfilePictureUrl" != nil ? "trainers.tarinerProfilePictureUrl" : ""), placeholderImage: UIImage.init(named: "tour_placeholder"), options: .handleCookies, completed: nil)
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let width = 0 + (collectionView.frame.width / 1)
    return CGSize(width: width,
                  height: 100)
}
}

extension DashboardBottomViewCollectionViewCell : UICollectionViewDelegate {

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    dashboardBottomDelegate?.didSelect(tem: indexPath.item)
}
}

Согласно коду, я вижу это:

enter image description here

Я могу видеть collectionView в Footer, но не могу изменить ориентацию на вертикальную вместо вида Grid следующим образом: enter image description here

Я застрял и не смог найти подсказки.Любой намек будет заметен.Спасибо

1 Ответ

0 голосов
/ 31 мая 2019

Вы пытались вместо этого ввести код установки DashboardBottomViewCollectionViewCell. init(frame:) в awakeFromNib()?

    override func awakeFromNib() {
        super.awakeFromNib()
        //setup cell and stuffs
    }

Также позвоните footerView?.reloadData(), чтобы перезагрузить содержимое.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...