2016-03-02 213 views
7

獲取可見IndexPath UICollectionView如何獲得visible IndexPath而在collectionViewscrolling,我提到很多link1link2indexPathForCell不斯威夫特支持。斯威夫特

+0

要獲取所有可見的細胞或特定的細胞 –

回答

4

試試這個

在委託

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) 
12

你試過委託功能?

public func indexPathsForVisibleItems() -> [NSIndexPath] 

collectionView.indexPathsForVisibleItems() 

這些都要給你你想要的東西。

1

斯威夫特,更安全的方式來獲得可見的項目

if let indexPaths = self.collectionView.indexPathsForVisibleItems { 
    //Do something with an indexPaths array. 
} 
0

可以使用UICollectionView的方法爲:

let indexPath = NSIndexPath(item: value1, section: value2)