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

Sas*_*ent 49 ios uicollectionview uicollectionviewcell

我如何才能找到indexPath用于cell在中间UICollectionView

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

只需按"废纸篓"按钮并确认删除.但现在它只删除了第一个cells.

- (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];
    }
}
Run Code Online (Sandbox Code Playgroud)

mic*_*tox 35

就像你自己一样,indexPathForItemAtPoint是找到你感兴趣的元素的索引路径的好方法.如果你的问题是:我怎么知道这一点的坐标?然后你应该尝试(使用你在代码片段中给出的相同名称):

initialPinchPoint = CGPointMake(self.collectionView.center.x + self.collectionView.contentOffset.x, 
                                self.collectionView.center.y + self.collectionView.contentOffset.y);
Run Code Online (Sandbox Code Playgroud)

  • collectionView.offset应该是collectionView.contentOffset (3认同)

Jla*_*lam 33

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

     NSIndexPath *centerCellIndexPath = 
[self.collectionView indexPathForItemAtPoint:
[self.view convertPoint:[self.view center] toView:self.collectionView]];
Run Code Online (Sandbox Code Playgroud)

这也是您实际尝试的正确表示:
1.获取视图控制器视图的中心点 - 也就是视觉中心点
2.将其转换为您感兴趣的视图的坐标空间 - 其中是集合视图
3.获取存在于给定位置的索引路径.

  • 由于原始问题要求在集合视图 * 中间的单元格,这会更正确:`CGPoint centerOfScrollableContent = [self.view convertPoint:self.collectionView.center toView:self.collectionView];` 重要如果collectionview 不是 `self.view` 的完整大小。 (2认同)

MCR*_*MCR 25

这是我在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")
}
Run Code Online (Sandbox Code Playgroud)

  • 优秀!我已经尝试了所有的解决方案,但没有一个只适用于你的 (2认同)

Bob*_*j-C 21

迅速:

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
}
}
Run Code Online (Sandbox Code Playgroud)

用法:

if let centerCellIndexPath: IndexPath  = collectionView.centerCellIndexPath {
                print(centerCellIndexPath)
            }
Run Code Online (Sandbox Code Playgroud)

斯威夫特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
}
}
Run Code Online (Sandbox Code Playgroud)


Dar*_*koM 8

谢谢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];

}
Run Code Online (Sandbox Code Playgroud)


Ale*_*tev 5

我做的像水平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()
}
Run Code Online (Sandbox Code Playgroud)