【问题标题】:Using PageControl in a CollectionViewCell on a ViewController在 ViewController 上的 CollectionViewCell 中使用 PageControl
【发布时间】:2017-05-29 11:54:20
【问题描述】:

我正在尝试为 CollectionViewCell 中的图像显示 PageControl。

一切似乎都正常工作——页数正确,甚至 print(currentPage) 显示正确的索引;但是,PageControl 没有显示正确的当前页面。

EncounterDetailViewController.swift

class EncounterDetailViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate {

// MARK: - Properties
var selectedEncounter: Encounter?
var currentPage = 0

// MARK: - UICollectionViewDataSource
func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return (selectedEncounter?.images.count)!
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! EncounterCollectionViewCell

    cell.imageView.sd_setImage(with: URL(string: (selectedEncounter?.images[indexPath.row])!))
    cell.pageControl.numberOfPages = (selectedEncounter?.images.count)!
    cell.pageControl.currentPage = self.currentPage

    return cell
}

// MARK: - ScrollView Delegate Method
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let pageWidth = scrollView.frame.width
    self.currentPage = Int((scrollView.contentOffset.x + pageWidth / 2) / pageWidth)
    print(currentPage)
}

}

EncounterCollectionViewCell.swift

class EncounterCollectionViewCell: UICollectionViewCell {

    // MARK: - Outlets
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var pageControl: UIPageControl!

}

【问题讨论】:

  • pageControl 显示哪个页面?
  • 您有一个pageControl,但您正在根据collectionView contentOffset 计算currentPage。为什么?
  • 我正在尝试根据每个水平滚动更改 pageControls currentPage。
  • 你的单元格中有scrollView吗?
  • 我相信是这样 - 集合视图单元格水平滚动。我可以通过将页面控件连接到 ViewController 来解决这个问题。

标签: ios swift uicollectionview uicollectionviewcell uipagecontrol


【解决方案1】:

不确定这是否是最佳实践方式,但我将 pageControl 连接到 EncounterDetailViewController 而不是单元格内。

class EncounterDetailViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate {

    @IBOutlet weak var pageControl: UIPageControl!

    // MARK: - Properties
    var currentPage = 0

    // MARK: - View did load
    override func viewDidLoad() {
        super.viewDidLoad()

        self.pageControl.numberOfPages = (selectedEncounter?.images.count)!

    }

    // MARK: - ScrollView Delegate Method
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let pageWidth = scrollView.frame.width
    self.currentPage = Int((scrollView.contentOffset.x + pageWidth / 2) / pageWidth)
    self.pageControl.currentPage = self.currentPage
}
}

【讨论】:

    猜你喜欢
    • 2018-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    相关资源
    最近更新 更多