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..
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.
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With