如何在CollectionView中添加页脚/页眉

时间:2019-05-31 00:59:04

标签: ios swift uicollectionview

代码段

Execution failed for task ':app:lintVitalRelease'.
> Lint infrastructure error
  Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
Caused by: java.lang.StackOverflowError

    at org.gradle.api.internal.provider.DefaultPropertyState.getOrNull(DefaultPropertyState.java:159)
.... (the above line is repeating tons of times)

显示收藏夹视图的页脚类

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)
}      
}

根据代码,我可以看到:

enter image description here

我可以在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) } } 中看到collectionView,但是不能将方向更改为垂直,而不是像这样的Grid视图: enter image description here

我被困住了,找不到任何提示。任何提示将是可观的。谢谢

1 个答案:

答案 0 :(得分:0)

您是否尝试将设置代码DashboardBottomViewCollectionViewCellinit(frame:)放在awakeFromNib()中?

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

还调用footerView?.reloadData()重新加载内容。