56

中间怎么indexPath找?cellUICollectionView

我有水平滚动,只有一个大cell的是可见的(侧面的另外两个cells 也是可见的)。我需要删除cell位于中心(意味着 - current cell)而不触摸它。

只需按“垃圾箱”按钮并确认删除。但现在它只删除 first 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];
    }
}
4

11 回答 11

40

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

initialPinchPoint = CGPointMake(self.collectionView.center.x + self.collectionView.contentOffset.x, 
                                self.collectionView.center.y + self.collectionView.contentOffset.y);
于 2013-10-09T07:57:07.727 回答
35

这是我在 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")
}
于 2016-10-14T02:53:22.070 回答
34

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

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

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

于 2014-09-23T00:37:53.333 回答
26

迅速:

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

斯威夫特 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
}
}
于 2014-12-08T09:50:53.677 回答
8

谢谢你!

我有多个 UICollectionView 可见单元格,需要将单元格定位在集合视图的中心。类似于 Adob​​e 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];

}
于 2014-05-19T13:56:00.787 回答
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()
}
于 2016-03-03T09:59:01.920 回答
2

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

于 2013-10-09T07:45:00.257 回答
1

对于某些可能在获取中心时遇到麻烦的人,请查看以下潜在解决方案:

 func centerCell()->UICollectionViewCell? {
      // Asuming your scrolling is horizontal
      let viewHorizontalCenter =  self.view.bounds.width / 2
      let center = CGPoint(x: viewHorizontalCenter, y: self.collectionView.center.y)

      let convertedPoint = self.view.convert(center, to: self.unitsCollectionView)

      let center = CGPoint(x: self.view.bounds.width / 2, y: self.unitsCollectionView.center.y)
      let convertedPoint = self.view.convert(center, to: self.unitsCollectionView)
      for cell in unitsCollectionView.visibleCells {
          if cell.frame.contains(convertedPoint) {
              print("Hello")
              return cell
          }
      }
      return nil
    }
于 2019-05-13T00:03:20.170 回答
0

试试这个协议...

protocol CollectionVisibleMidCell {}
extension CollectionVisibleMidCell where Self: UICollectionView {

    func getMidVisibleIndexPath() -> IndexPath? {
        var visibleRect = CGRect()
        visibleRect.origin = self.contentOffset
        visibleRect.size = self.bounds.size
        let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
        guard let indexPath = self.indexPathForItem(at: visiblePoint) else { return nil }
        return indexPath
    }
}

extension UICollectionView: CollectionVisibleMidCell {}
于 2018-02-19T00:36:24.397 回答
0

斯威夫特 4

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    let indexPath = collectionView.indexPathForItem(at: collectionView.bounds.center)        
}
于 2018-10-17T16:37:45.547 回答
0

基于@micantox 的回答。更新了Swift 5的代码

let initialPinchPoint = CGPoint(x: collectionView.center.x + collectionView.contentOffset.x,
                                y: collectionView.center.y + collectionView.contentOffset.y)
于 2019-05-09T05:44:44.310 回答