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.
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..
You can compare which view is scrolling now inside
scrollViewDidScroll
.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 thecollectionViewCell
. So it should be look something like this.Now call this method in your tableView's datasource method like this.