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

时间:2013-10-09 07:25:38

标签: ios uicollectionview uicollectionviewcell

如何在indexPath中找到cell的{​​{1}}?

我有水平滚动,只有一个大的UICollectionView可见(两侧的其他cell部分也是可见的)。 我需要删除位于中心的cell(表示当前cell),而不会触及它。

仅按“废纸篓”按钮并确认删除。但现在它只删除了第一个cell

cell

11 个答案:

答案 0 :(得分:34)

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

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

答案 1 :(得分:33)

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

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

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

答案 2 :(得分:23)

这是我在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")
}

答案 3 :(得分:19)

夫特:

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

答案 4 :(得分: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];

}

答案 5 :(得分: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()
}

答案 6 :(得分:2)

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

答案 7 :(得分: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 {}

答案 8 :(得分:0)

雨燕4

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    let indexPath = collectionView.indexPathForItem(at: collectionView.bounds.center)        
}

答案 9 :(得分:0)

基于@micantox的答案。 Swift 5

的更新代码
let initialPinchPoint = CGPoint(x: collectionView.center.x + collectionView.contentOffset.x,
                                y: collectionView.center.y + collectionView.contentOffset.y)

答案 10 :(得分:0)

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

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
}