【问题标题】:How to keep the didSelect action in UICollectionView in Objective-c?如何在 Objective-c 的 UICollectionView 中保留 didSelect 操作?
【发布时间】:2017-03-08 02:01:20
【问题描述】:

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

【问题讨论】:

  • 这段代码可能还不足以找到问题所在。请添加collection view的dataSource和delegate方法。
  • didSelectItemAtIndexPath 仅在用户选择项目时调用。滚动不会调用那个方法
  • 我正在添加我的代码,我不想在滚动时调用 didSelect。我的问题是重用和重新绘制单元格,因为我的第一个单元格失去了添加的功能。我认为正在写其他索引,当我点击 0 位置时确实是 1 个单元格索引
  • @user3745888 我相信theMachSystem 的答案实际上可能是您正在寻找的解决方案。单元的可重用性可能会导致您遇到的问题

标签: ios objective-c uicollectionview uicollectionviewcell prepareforreuse


【解决方案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];
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-05
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 2012-03-01
      相关资源
      最近更新 更多