Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get indexPath for cell which is located in the center of UICollectionView

How can i find indexPath for cell in the middle of UICollectionView?

I have horizontal scrolling and only one big cell is visible (partially two other cells on the sides are visible as well). I need to delete cell located in the center (means - current cell) without touching it.

Only pressing "Trash" button and confirm Delete. But now it delete only 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];     } } 
like image 588
Sasha Prent Avatar asked Oct 09 '13 07:10

Sasha Prent


People also ask

How do I get the IndexPath of a cell Swift?

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.

How do I create an IndexPath in Objective C?

To create an IndexPath in objective C we can use. NSIndexPath *myIP = [NSIndexPath indexPathForRow: 5 inSection: 2] ; To create an IndexPath in Swift we can use.


2 Answers

Like you did yourself, indexPathForItemAtPoint is a good way of finding the index path of the element you're interested in. If your question is: how do i know the coordinates of this point? Then you should try with (using the same name you gave it in your code snippet):

initialPinchPoint = CGPointMake(self.collectionView.center.x + self.collectionView.contentOffset.x,                                  self.collectionView.center.y + self.collectionView.contentOffset.y); 
like image 86
micantox Avatar answered Sep 22 '22 07:09

micantox


Here's what I did in 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") } 
like image 37
MCR Avatar answered Sep 22 '22 07:09

MCR