I tried this to get the index of the cell at the center of the collection view using the uicollectionview extension, but I always get zero instead of index. How can i fix this?
extension UICollectionView { var centerPoint : CGPoint { get { return CGPoint(x: self.center.x + self.contentOffset.x, y: self.center.y + self.contentOffset.y); } } var centerCellIndexPath: NSIndexPath? { if let centerIndexPath: NSIndexPath = self.indexPathForItemAtPoint(self.centerPoint) { return centerIndexPath } return nil } }
then: in a UIViewController in a random method, I have this:
if let centerCellIndexPath: NSIndexPath = collectionTemp!.centerCellIndexPath { print(centerCellIndexPath) } else { println("nil") }
the index path is always zero, and I donβt understand why, because the cells are displayed in order, everything is fine except for this.
source share