gpt4 book ai didi

ios - 如何根据 CollectionViewCell 的项目更改 UIPageControl 的当前位置

转载 作者:行者123 更新时间:2023-11-29 11:58:56 25 4
gpt4 key购买 nike

我的 View 层次结构是这样的:

TableView-->tableViewCell-->CollectionView-->CollectionViewCell-->imageView

在我的 tableViewCell 中我有一些其他项目(textView、标签和 UIPageControl)所以现在我试图根据 CollectionViewCell 的项目(与 carousel 相同)更改 PageControl 的 currentPage 但我不知道为什么 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 后记住单元格中 Item 的当前位置)

override func tableView(tableView: UITableView,
willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath) {

guard let cell = cell as? NotesTableViewCell else { return }

cell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)

cell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0
cell.pageControll.currentPage = calculateCurrentPage(storedOffsets[indexPath.row] ?? 0)

}

上面是从 (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
}
}

这是怎么回事?或者怎么做?

更新额外代码:

//在我的tableViewCell中 函数 setCollectionViewDataSourceDelegate > (dataSourceDelegate: D, forRow row: Int) {

    collectionView.delegate = dataSourceDelegate
collectionView.dataSource = dataSourceDelegate
collectionView.tag = row
collectionView.reloadData()
}

//在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)
}
}
}

最佳答案

检查#1

只要在 collectionView 实例上设置了 UICollectionViewDelegate,您就会收到 UICollectionView 滚动事件的回调。

似乎您可能缺少在后续调用中设置 UICollectionViewDelegate

cell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)

你能确认你正在接收 UICollectionView 滚动事件的回调吗?

检查#2

假设您现在可以正确接收回调,您能否检查您的逻辑是否与页面索引一起正常工作?一个好办法是添加一个打印语句,它会显示您正在计算的页面索引。

let pageIndex = Int(scrollView.contentOffset.x / self.view.frame.size.width)
print("pageIndex == \(pageIndex)")

检查#3

假设您计算正确,您能否检查 cell.pageControl 是否正确填充了您需要更新的 UIPageControl 实例。

也许您需要检查您的 IBOutlet 连接?

检查 #4

prepareForReuse() 回调中,您需要确保将 pageControl 设置为某个初始值,例如 0。

在更新 pageIndex 时添加一个小的延迟可能会起作用,如果您看到不一致,例如有时更新有时不更新。

希望对您有所帮助。

关于ios - 如何根据 CollectionViewCell 的项目更改 UIPageControl 的当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37881072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com