我尝试使用UICollectionView的扩展来获取集合视图中心单元格的索引,但我总是得到nil而不是索引。我该如何解决这个问题?
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
}
}
接下来:在一个UIViewController的随机方法中,我有如下代码:
if let centerCellIndexPath: NSIndexPath = collectionTemp!.centerCellIndexPath {
print(centerCellIndexPath)
} else {
println("nil")
}
索引路径始终为空,我不知道为什么,因为单元格按顺序显示,除了这个问题其他都很好。