如何获取位于UICollectionView中心的单元格的indexPath

我如何findindexPath中的cellUICollectionView

我有水平滚动,只有一个大的cell是可见的(部分两侧其他cell也是可见的)。 我需要删除位于中心的cell (意味着 – 当前cell )而不用触摸它。

只需按“垃圾箱”button并确认删除。 但现在它只删除第一个cell

 - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex { if (buttonIndex == actionSheet.destructiveButtonIndex) { initialPinchPoint = ???????? self.tappedCellPath = [self.collectionView indexPathForItemAtPoint:initialPinchPoint]; Technique *technique = [arrayOfTechnique objectAtIndex:self.tappedCellPath.row]; [self deleteData:[NSString stringWithFormat:@"DELETE FROM TECHNIQUES WHERE TECHNIQUENAME IS '%s'",[technique.techniquename UTF8String]]]; [arrayOfTechnique removeObjectAtIndex:self.tappedCellPath.row]; //[arrayOfTechnique removeObjectAtIndex:[custom_layout ]]; [self removeImage:technique.techniquename]; [self.collectionView performBatchUpdates:^{ [self.collectionView deleteItemsAtIndexPaths:[NSArrayarrayWithObject:self.tappedCellPath]]; } completion:nil]; [self checkArrayCount]; } } 

就像你自己做的那样, indexPathForItemAtPoint是find你感兴趣的元素的索引path的好方法。如果你的问题是:我怎么知道这个点的坐标? 那么你应该尝试(使用你在代码片段中给出的同名):

 initialPinchPoint = CGPointMake(self.collectionView.center.x + self.collectionView.contentOffset.x, self.collectionView.center.y + self.collectionView.contentOffset.y); 

这将是更好的代码,因为它更清晰,更容易阅读,所有的内容偏移计算是多余的:

  NSIndexPath *centerCellIndexPath = [self.collectionView indexPathForItemAtPoint: [self.view convertPoint:[self.view center] toView:self.collectionView]]; 

这也是您实际尝试做的正确表示:
1.以您的视angular控制者的视angular为中心点 – 又名视觉中心点
2.将其转换为您感兴趣的视图的坐标空间 – 这是收集视图
3.获取给定位置处的索引path。

迅速:

 extension UICollectionView { var centerPoint : CGPoint { get { return CGPoint(x: self.center.x + self.contentOffset.x, y: self.center.y + self.contentOffset.y); } } var centerCellIndexPath: IndexPath? { if let centerIndexPath: IndexPath = self.indexPathForItemAtPoint(self.centerPoint) { return centerIndexPath } return nil } } 

用法:

 if let centerCellIndexPath: IndexPath = collectionView.centerCellIndexPath { print(centerCellIndexPath) } 

Swift 3:

 extension UICollectionView { var centerPoint : CGPoint { get { return CGPoint(x: self.center.x + self.contentOffset.x, y: self.center.y + self.contentOffset.y); } } var centerCellIndexPath: IndexPath? { if let centerIndexPath = self.indexPathForItem(at: self.centerPoint) { return centerIndexPath } return nil } } 

这就是我在Swift 3中所做的

 private func findCenterIndex() { let center = self.view.convert(self.collectionView.center, to: self.collectionView) let index = collectionView!.indexPathForItem(at: center) print(index ?? "index not found") } 

谢谢micantox!

我有UICollectionView的多个可见单元格,并需要将单元格放置在集合视图的中心。 与Adobe Content Viewer类似的东西。 如果有人在类似的情况下苦苦挣扎:

 #pragma mark - UIScrollViewDelegate methods - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ CGPoint centerPoint = CGPointMake(self.pageContentCollectionView.center.x + self.pageContentCollectionView.contentOffset.x, self.pageContentCollectionView.center.y + self.pageContentCollectionView.contentOffset.y); NSIndexPath *centerCellIndexPath = [self.pageContentCollectionView indexPathForItemAtPoint:centerPoint]; [self.pageContentCollectionView scrollToItemAtIndexPath:centerCellIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES]; } 

我做了水平的UICollectionView我使用Swift 2.x。

 private func findCenterIndex() { let collectionOrigin = collectionView!.bounds.origin let collectionWidth = collectionView!.bounds.width var centerPoint: CGPoint! var newX: CGFloat! if collectionOrigin.x > 0 { newX = collectionOrigin.x + collectionWidth / 2 centerPoint = CGPoint(x: newX, y: collectionOrigin.y) } else { newX = collectionWidth / 2 centerPoint = CGPoint(x: newX, y: collectionOrigin.y) } let index = collectionView!.indexPathForItemAtPoint(centerPoint) print(index) } override func scrollViewDidEndDecelerating(scrollView: UIScrollView) { findCenterIndex() } 

UICollectionView有一个方法- (NSIndexPath *)indexPathForItemAtPoint:(CGPoint)point
此方法返回指定点上项目的索引path。 您可以计算代表UICollectionView中心的点,然后使用该CGPoint和此方法检索要删除的项目的索引path。