Ios 如何根据CollectionViewCell';更改UIPageControl的当前位置;s项目
我的视图层次结构类似于:Ios 如何根据CollectionViewCell';更改UIPageControl的当前位置;s项目,ios,swift2,uicollectionviewcell,uipagecontrol,Ios,Swift2,Uicollectionviewcell,Uipagecontrol,我的视图层次结构类似于: TableView-->tableViewCell-->CollectionView-->CollectionViewCell-->imageView 在我的tableViewCell中,我有一些其他项目(textView、labels和UIPageControl),因此现在我尝试根据CollectionViewCell的项目更改PageControl的当前页面(与carousel相同),但我不知道为什么UIPageControl不更改其位置这是我尝试的: overri
TableView-->tableViewCell-->CollectionView-->CollectionViewCell-->imageView
在我的tableViewCell中,我有一些其他项目(textView、labels和UIPageControl),因此现在我尝试根据CollectionViewCell的项目更改PageControl的当前页面(与carousel相同),但我不知道为什么UIPageControl不更改其位置这是我尝试的:
override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
// here scrollView is my collectionView
if scrollView != tableView {
pagerControll.currentPage = Int(scrollView.contentOffset.x / self.view.frame.size.width)
}
}
我也尝试过这样做(在滚动tableView后记住单元格中项的当前位置)
上面,我从一个数组(contentOffset)中获取每行的contentOffset,这样我就可以显示CollectionViewCell项的先前位置,当tableView重用该单元格时,它对我的collectionView单元格项工作正常,但对我的UIPageControl
func calculateCurrentPage(offSet : CGFloat) -> Int{
if offSet >= self.view.frame.size.width && offSet < (self.view.frame.size.width*2) {
return 1
}else if offSet >= (self.view.frame.size.width*2) {
return 2
}else {
return 0
}
}
//在TableView中
extension NotesTableViewController: UICollectionViewDelegate, UICollectionViewDataSource , UICollectionViewDelegateFlowLayout {
func collectionView(collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
pagerControll.numberOfPages = attachedImgUrlDict[collectionView.tag]!.count
return attachedImgUrlDict[collectionView.tag]!.count
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let newSize = CGSizeMake(collectionView.frame.size.width , collectionView.frame.size.height)
return newSize
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0, 0, 0, 0)
//t,l,b,r
}
override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
if scrollView != tableView {
if scrollView.contentOffset.x >= 320 && scrollView.contentOffset.x < 640{
pagerControll.currentPage = 1
}else if scrollView.contentOffset.x >= 600 {
pagerControll.currentPage = 2
}
//pagerControll.currentPage = Int(scrollView.contentOffset.x / self.view.frame.size.width)
print(scrollView.contentOffset)
}
}
}
扩展说明StableViewController:UICollectionViewDelegate、UICollectionViewDataSource、UICollectionViewDelegateFlowLayout{
func collectionView(collectionView:UICollectionView,
NumberOfItemsSection:Int)->Int{
PagerControl.numberOfPages=attachedImgUrlDict[collectionView.tag]!.count
return attachedImgUrlDict[collectionView.tag]!.count
}
func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,SizeFormiteIndeXPath indexPath:NSIndexPath)->CGSize{
let newSize=CGSizeMake(collectionView.frame.size.width,collectionView.frame.size.height)
返回新闻大小
}
func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,InsertForSectionAtIndex节:Int)->UIEdgeInsets{
返回UIEdgeInsetsMake(0,0,0,0)
//t、 l,b,r
}
重写函数ScrollViewDiEndDecelling(scrollView:UIScrollView){
如果scrollView!=表格视图{
如果scrollView.contentOffset.x>=320&&scrollView.contentOffset.x<640{
PagerControl.currentPage=1
}否则,如果scrollView.contentOffset.x>=600{
PagerControl.currentPage=2
}
//PagerControl.currentPage=Int(scrollView.contentOffset.x/self.view.frame.size.width)
打印(scrollView.contentOffset)
}
}
}
检查#1
在collectionView
实例上设置UICollectionViewDelegate
后,您将收到UICollectionView
滚动事件的回调
在下面的调用中,您可能缺少设置UICollectionViewDelegate
cell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
您是否可以验证是否接收到UICollectionView
scroll事件的回调
支票#2
假设您现在正确地接收回调,您可以检查您的逻辑是否与页面索引正常工作吗?一个好方法是添加一个print语句,它将显示您正在计算的页面索引
let pageIndex = Int(scrollView.contentOffset.x / self.view.frame.size.width)
print("pageIndex == \(pageIndex)")
支票#3
假设您计算正确,是否可以检查cell.pageControl
是否正确填充了需要更新的UIPageControl
实例
也许你需要检查你的IBOutlet
连接
支票#4
在prepareforeuse()
回调中,您需要确保pageControl
设置为一些初始值,如0
在更新页面索引
时添加一个小延迟,如果您看到不一致的情况,例如有时正在更新,有时不更新,则可能会起作用
希望有帮助。是的,我会,因为这就是我从imageView更改图像的方式,顺便说一下,我将如何向您展示?@remyboys
UICollectionViewDataSource
和UICollectionViewDelegate
是两件不同的事情。你确定两个都设置好了吗?请共享setCollectionViewDataSourceDelegate()方法中的代码。请查看更新的问题@TarunTyagihey man我检查了它,发现有奇怪的事情发生,当第一次加载表格并更改图像时,PageControl.currentpage不会更改,但当我向下滚动到底部,然后滚动到顶部并更改图像时,一切正常,知道为什么会发生这种情况吗?似乎与TableView有关lifeCycle@remyboys这是第一次!当单元格准备在override prepareforeuse
回调中重用时,您可以检查pageControl
是否正确加载。或者,当您更新pageIndex
时,添加0.01-0.1秒这样的小延迟可能会起作用,这似乎是应用的逻辑时序不一致。刚刚更新了答案。看一下支票4。
cell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
let pageIndex = Int(scrollView.contentOffset.x / self.view.frame.size.width)
print("pageIndex == \(pageIndex)")