[英]UICollectionView: Get indexPath of Cell that is not visible
[英]Get Visible IndexPath UICollectionView in Swift
你試過代理功能嗎?
public func indexPathsForVisibleItems() -> [NSIndexPath]
要么
collectionView.indexPathsForVisibleItems()
這些必須給你你想要的。
嘗試這個
代表
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
for cell in yourCollectionViewname.visibleCells() as [UICollectionViewCell] {
let indexPath = yourCollectionViewname.indexPathForCell(cell as UICollectionViewCell)
NSLog("%@", indexPath)
}
}
選擇-2
按鈕單擊
var point : CGPoint = sender.convertPoint(CGPointZero, toView:yourCollectionViewname)
var indexPath =yourCollectionViewname!.indexPathForItemAtPoint(point)
獲取所有項目
你可以使用indexPathsForVisibleRows
返回索引路徑數組,每個索引路徑標識接收器中的可見行。
- (NSArray *)indexPathsForVisibleItems;
var visible: [AnyObject] = yourCollectionViewname.indexPathsForVisibleItems
var indexpath: NSIndexPath = (visible[0] as! NSIndexPath)
快速, 更安全的方式獲得可見項目 :
if let indexPaths = self.collectionView.indexPathsForVisibleItems {
//Do something with an indexPaths array.
}
這樣可以正常工作。
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let visibleIndex = Int(targetContentOffset.pointee.x / collectionView.frame.width)
print(visibleIndex)
}
您可以使用UICollectionView的方法:
let indexPath = NSIndexPath(item: value1, section: value2)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.