私のプロジェクトでは、collectionView 内で tableView を使用し、pageControl も使用しました。コレクションビューを横にスワイプした際にpageControlも切り替えたいのですがうまくいきません。スワイプscrollViewDidScroll
の切り替え方法を使いpageControl
ました。collectionView
しかし問題は、tableView scrollsscrollViewDidScroll
メソッドが再び呼び出されるときです。この問題を解決するための別の方法や解決策はありますか。
以下のリンクを確認してください。
TableView メソッド
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 メソッド
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 メソッド
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
}
助けてくれてありがとう..