collectionView必须点击两次才跳转
今天遇到一个很奇怪的现象:collectionView必须点击两次才能跳转。具体看代码:
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"该项被触发"); } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"选中了该项"); self.hidesBottomBarWhenPushed = YES; ReadingViewController *vc = [ReadingViewController new]; [self.navigationController pushViewController:vc animated:YES]; }
搜了一下资料发现didDelect是取消选定,于是将该行代码删除:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"选中了该项"); self.hidesBottomBarWhenPushed = YES; ReadingViewController *vc = [ReadingViewController new]; [self.navigationController pushViewController:vc animated:YES]; }
运行发现仍然是需要点击两次才能出发didSelect方法,查找一翻资料后发现在collectionView的初始化时多加了一行代码,将其注释就可以正常运行了:
//self.collectionView.allowsMultipleSelection = YES;
该句代码通常适用有多行选择的需求时,将其注释后,就彻底解决collectionView点击两次才跳转的问题咯。