0

我有一个 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];
}
4

2 回答 2

1

问题出在这段代码上:

- (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;

    return cell;
  }

因为细胞是可重复使用的。因此,您之前禁用的单元格(例如 cell0)将继续被重用。例如,当您滚动时,该 cell0 将变为 cell11。但是,它的设置仍然被禁用。

您需要添加一个 else 语句来删除这样的禁用设置。

if([indexPath row]==2){
      [cell setUserInteractionEnabled:NO];//Here I set disable Could be the problem caused by this??
}
else{
      [cell setUserInteractionEnabled:YES];
}
于 2016-10-26T00:37:28.837 回答
0

当一个单元格滚动到屏幕外时,它可能会被释放或(更有可能)回收,因此您在该单元格中的任何状态在其离开屏幕后都不会保留。解决方案是您应该检查是否选择了单元格并在cellForRowAtIndexPath中更新其外观。您可以让didSelectItemAtIndexPathdidDeselectItemAtIndexPath调用reloadRowsAtIndexPathcellForRowAtIndexPath将在一个地方处理所有突出显示的外观逻辑。

于 2016-10-25T21:39:28.117 回答