How to use the pageControl in Collectionview Inside the Tableview?

ashmi123 picture ashmi123 · Dec 21, 2016 · Viewed 8.2k times · Source

In my project I used a tableView inside a collectionView and also I used a pageControl. When I swipe the collection view horizontally, I want to switch the pageControl too, but it is not working properly. I used scrollViewDidScroll method for switching the pageControl as the collectionView Swipes. But the problem is, when the tableView scrolls scrollViewDidScroll method will again call. So is there any different way or any solution for fix this issue.

Check the below link.

enter link description here

TableView Method

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
{
     return model.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{
     cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FbHomeCell
     cell.btnPushImage.tag = indexPath.row
     cell.collectionView.tag = indexPath.row
     return cell
}

CollectionView Method

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
     return model[collectionView.tag].count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
      collCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! FBHomeCollectionCell    
      collCell.backgroundColor = model[collectionView.tag][indexPath.item]    
      collCell.pageControlSwipe.numberOfPages = Int(collectionView.contentSize.width/collectionView.frame.size.width)                       
      return collCell
 }

Scroll View Delegate Method

func scrollViewDidScroll(_ scrollView: UIScrollView)
{
     let pageWidth = scrollView.frame.width
     collCell.pageControlSwipe.currentPage = Int((scrollView.contentOffset.x + pageWidth / 2) / pageWidth)

     collCell = cell.collectionView.cellForItem(at: IndexPath (item: scrollView.tag, section: 0)) as! FBHomeCollectionCell

 }

Thanks and Appreciate for help..

Answer

Nirav D picture Nirav D · Dec 21, 2016

You can compare which view is scrolling now inside scrollViewDidScroll.

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView == self.collectionView { //Your scrollView outlet
        //Your code here
    }
}

Edit: First of all you need to implement collectionView's datasource and delegate method with your tableViewCell not with your ViewController also you need to put pageControlSwipe in tableViewCell not in the collectionViewCell. So it should be look something like this.

class FbHomeCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate {

    @IBOutlet var collectionView: UICollectionView!
    @IBOutlet var pageControlSwipe: UIPageControl!
    @IBOutlet var btnPushImage: UIButton!

    var colorArray = [UIColor]()              
    var currentPage = 0

    func setCollectionViewWith(colorArray: [UIColor]) {
         self.colorArray = colorArray
         self.collectionView.isPagingEnabled = true
         self.collectionView.datasource = self
         self.collectionView.delegate = self
         self.collectionView.reloadData()
    }        

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let collCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! FBHomeCollectionCell
        collCell.backgroundColor = self.colorArray[indexPath.item]
        self.pageControlSwipe.numberOfPages = colorArray.count
        return collCell
    }

    //ScrollView delegate method
    func scrollViewDidScroll(_ scrollView: UIScrollView)
    {
        let pageWidth = scrollView.frame.width
        self.currentPage = Int((scrollView.contentOffset.x + pageWidth / 2) / pageWidth)
        self.pageControlSwipe.currentPage = self.currentPage
    }
}

Now call this method in your tableView's datasource method like this.

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return model.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FbHomeCell
        cell.btnPushImage.tag = indexPath.row
        cell.collectionView.tag = indexPath.row
        cell.setCollectionViewWith(colorArray: model[indexPath.row])
        return cell
    }