我有一个 UICollectionView 并从我的 cellForItemAtIndexPath 12 个单元格中添加。当我滚动查看向下单元格时,所有功能都被保留,但是当我再次滚动向上时,某些单元格不会执行在 didSelectItemAtIndexPath 中加载的函数。
我设置禁用了一些行。但不是我单击的行为什么会这样?重用电池可能是一个糟糕的准备吗?
我正在尝试重用功能,但这只会影响将单元格绘制错误或在另一个位置上,并且在 didSelectItemAtIndexPath 中添加的功能不起作用:
[self.myCollectionView reloadData];
[self.myCollectionView layoutIfNeeded];
NSArray *visibleItems = [self.myCollectionView indexPathsForVisibleItems];
NSIndexPath *currentItem = [visibleItems objectAtIndex:0];
NSIndexPath *nextItem = [NSIndexPath indexPathForItem:currentItem.item + 1 inSection:currentItem.section];
[self.myCollectionView scrollToItemAtIndexPath:nextItem atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
当我滚动并单击一个单元格时,这不会打开我的 secondViewController,我认为该单元格的索引错误已被禁用。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyViewController *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];
switch ([indexPath row]) {
case 0:
titleCell= @"Title0";
detailCell=@"Detail0";
if([indexPath row]==2){
[cell setUserInteractionEnabled:NO];//Here I set disable Could be the problem caused by this??
}
[cell.image setHidden:YES];
cell.image.contentMode = UIViewContentModeScaleAspectFit;
break;
//Here other cases with the same structure
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[collectionView deselectItemAtIndexPath:indexPath animated:NO];
MySecondClass *secondClass = [self.storyboard instantiateViewControllerWithIdentifier:@"myVC"];
[self.navigationController pushViewController:secondClass animated:YES];
}
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
//Here assign yellow background color to first row and white color to the second row
}
在我的单元格中,我添加了 prepareForReuse 但这不起作用......
- (void)prepareForReuse {
[super prepareForReuse];
[self removeFromSuperview];
[self setNeedsLayout];
}