11

私のプロジェクトでは、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

 }

助けてくれてありがとう..

4

1 に答える 1

18

内で現在スクロールしているビューを比較できますscrollViewDidScroll

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

編集:まず、ViewController ではなく tableViewCell を使用して collectionView のデータソースとデリゲート メソッドを実装する必要がありpageControlSwipeますcollectionViewCell。したがって、このように見えるはずです。

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

次のように、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
        cell.setCollectionViewWith(colorArray: model[indexPath.row])
        return cell
    }
于 2016-12-21T05:38:57.487 に答える