Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the pageControl in Collectionview Inside the Tableview?

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..

like image 628
ashmi123 Avatar asked Dec 21 '16 05:12

ashmi123


People also ask

What is the difference between Collectionview and Tableview?

Table view presents data in multiple rows arranged in a single column, the cell design is fit as a row. However, collection view can create rows and columns, it can design cells in various ways, even if they are not necessarily rows. It's also the biggest feature of a collection view cell.

Should I use Tableview or Collectionview?

1. The collection view is much more robust in terms of layout features and other things like animations. Tableiw is a simple list, which displays single-dimensional rows of data. It's smooth because of cell reuse and other magic.


1 Answers

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
    }
like image 174
Nirav D Avatar answered Oct 06 '22 22:10

Nirav D