6

I want to show banner like this:

enter image description here

My approach is adding a CollectionView as a TableViewHeader

My code:

extension HomeViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func configureHeaderView() {

        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

        let headerView = UICollectionView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: headerHeight), collectionViewLayout: layout)
        headerView.backgroundColor = .blue
        headerView.isPagingEnabled = true
        headerView.isUserInteractionEnabled = true

        headerView.dataSource = self
        headerView.delegate = self
        headerView.register(BannerCollectionViewCell.self, forCellWithReuseIdentifier: BannerCollectionViewCell.reuseIdentifier)
        headerView.showsHorizontalScrollIndicator = false

        tableView.tableHeaderView = headerView
    }

    // MARK: UICollectionViewDataSource
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: BannerCollectionViewCell.reuseIdentifier, for: indexPath) as! BannerCollectionViewCell
        return cell
    }

    // MARK: UICollectionViewDelegateFlowLayout
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: UIScreen.main.bounds.width, height: headerHeight)
    }
}

My BannerCollectionViewCell has a default image.

class BannerCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var bannerImageView: UIImageView!

}

enter image description here

But I don't see that image on my header. It just show an empty header.

10
  • Have you tried using the delegate method viewForHeaderInSection? Commented Jan 13, 2017 at 9:42
  • Not yet, I have set tableView.tableHeaderView = headerView, are they equivalent to each other? Commented Jan 13, 2017 at 9:43
  • in cellForItemAt indexPath , you have to set image. like cell.imageView.image = "abc.png" Commented Jan 13, 2017 at 9:46
  • Not exactly, but if you only have 1 section both works. Commented Jan 13, 2017 at 9:47
  • #Ben Ong, I have only 1 section, #the_dahiya_boy, I have set default image. Commented Jan 13, 2017 at 9:47

2 Answers 2

7

you use the NIB, so you should use func register(UINib?, forCellWithReuseIdentifier: String) instead of func register(AnyClass?, forCellWithReuseIdentifier: String)

Sign up to request clarification or add additional context in comments.

3 Comments

Post answer only if you have any answer regarding to question otherwise write in comment.
This is the correct answer. Thanks for your all helps :)
@zhongwuzw Thanks I'm having issues with Autolayout . Could you help me?
-3

Maybe you're looking for an image pager like KIImagePager on the top instead of a collection view. You can also create the same using inbuilt PageViewController.

You can add a TableView separately below this.

1 Comment

He is using UIcollectionview by disabling verticle move, not PageController

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.