Call to didHighlightItemAtIndexPath without calling didSelectItemAtIndexPath for UICollectionView

I have a UICollectionView where I reimplemented hitTest: withEvent: in my UICollectionViewCells to allow padding outside cells to register as branches on cells.

When I do this and I click only those cells that are now registered as hits, I get calls to didHighlightItemAtIndexPath and didUnhighlightItemAtIndexPath, but I do not get a call to didSelectItemAtIndexPath. If I touch inside the cell, I get all the expected selection and select the element calls, as before.

I don't have custom gesture recognizers, and I don't override touchhesBegan or anything like that.

Does anyone know under what conditions you get a call for didHighlightItemAtIndexPath without calling didSelectItemAtIndexPath? Is there a way to get my didSelectItemAtIndexPath request? Thanks.

EDIT

I forgot to mention that my UICollectionView is in Today widgets, so it is contained in the scroll view of the notification center. If I translate my selected code to the didUnhighlightItemAtIndexPath file, it is called when you click outside the cell, but as a result you cannot scroll through the Notification Center without selecting one of the cells.

So, maybe the difference between the selections and the choices I experience here has to do with the fact that the scroll view overrides the selection outside the cell?

+6
source share
1 answer

Ok, I understood what was going on.

I added a new UITapGestureRecognizer to my UICollectionView. Implementing this, as it led me to the solution:

- (void)cellSingleTap:(UITapGestureRecognizer *)sender { CGPoint point = [sender locationInView:collectionView_]; NSIndexPath *indexPath = [collectionView_ indexPathForItemAtPoint:point]; [ .... ] } 

When I checked the points returned when I got the selection, but there was no choice, it became obvious that this happened when the point clicked on it was within the sectional attachments of the collection view layout. And when the taps were on the section inserts, calls to indexPathForItemAtPoint returned zero.

Thus, basically, a collection view will highlight, but not select, taps that are outside the cells but are inside its sections. As long as the outputs are outside the cells and not inside the insert, these taps will result in didSelectItemAtIndexPath calls.

Since I would like the taps inside the inserts to be considered labels on the cells, I was able to solve this problem by adjusting the connection points before my call to indexPathForItemAtPoint.

+1
source

Source: https://habr.com/ru/post/985310/


All Articles