由於滾動視圖對細胞重疊...最好的辦法是添加敲擊手勢上的UIScrollView諸如此類,
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureAction:)];
[recognizer setNumberOfTapsRequired:1];
self.scrollViu.userInteractionEnabled = YES;
[self.scrollViu addGestureRecognizer:recognizer];
添加上面的代碼中cellForItemAtIndexPath方法和寫入動作手勢的方法例如像
-(void)gestureAction:(UITapGestureRecognizer *) sender
{
CGPoint touchLocation = [sender locationOfTouch:0 inView:self.YourCollectionViewName];
NSIndexPath *indexPath = [self.YourCollectionViewName indexPathForRowAtPoint:touchLocation];
NSLog(@"%d", indexPath.item);
}
在上面的手勢(動作)方法中,您可以獲取與didSelectItemAtIndexPath相同的indexPath。
你設置了uicollectionView的「委託」嗎?你是否符合你的「viewcontroller」? –
是的。當我觸摸100x100矩形內的單元格時調用該方法。即scrollview的可見部分 – Neenu