swift - 如何在 Tableview 中的 Collectionview 中使用 pageControl?

标签 swift uitableview uicollectionview swift3 pagecontrol

在我的项目中,我在 collectionView 中使用了 tableView,还使用了 pageControl。当我水平滑动 Collection View 时,我也想切换 pageControl,但它不能正常工作。我使用 scrollViewDidScroll 方法将 pageControl 切换为 collectionView 滑动。但问题是,当tableView滚动时,scrollViewDidScroll方法会再次调用。那么是否有任何不同的方法或任何解决方案来解决这个问题。

检查以下链接。

enter link description here

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
 }

ScrollView 委托(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

 }

感谢并感谢您的帮助..

最佳答案

您可以在 scrollViewDidScroll 中比较哪个 View 正在滚动。

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

编辑:首先,您需要使用 tableViewCell 而不是 ViewController 来实现 collectionView 的数据源和委托(delegate)方法,您还需要将 pageControlSwipe 放在 tableViewCell 中而不是 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
    }

关于swift - 如何在 Tableview 中的 Collectionview 中使用 pageControl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41255707/

相关文章:

swift - 如何在 Swift 3.0 中拖放 Sprite ?

swift - 删除解析 PF 用户帐户

swift - 您应该使用外部文件来存储大数据吗?

ios - 如何增加 UICollectionView 的 NSIndexPath

ios - 如何使用 UIStackView 更改 UITableViewCell 的布局

uitableview - Collection View : didSelectItemAtIndexPath: does not get called

ios - UITableView 返回不同的单元格

ios - 在tableViewCell中显示NSArray

swift - UICollectionViewDelegateFlowLayout 不更改单元格边界

iphone - block 完成执行后如何更新 CollectionView numberOfItemsInSection