【发布时间】: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,但您正在根据collectionViewcontentOffset计算currentPage。为什么? -
我正在尝试根据每个水平滚动更改 pageControls currentPage。
-
你的单元格中有
scrollView吗? -
我相信是这样 - 集合视图单元格水平滚动。我可以通过将页面控件连接到 ViewController 来解决这个问题。
标签: ios swift uicollectionview uicollectionviewcell uipagecontrol