如何在Objective-c中的UICollectionView中保留didSelect操作?

时间:2016-10-25 21:14:10

标签: ios objective-c uicollectionview uicollectionviewcell prepareforreuse

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

当我滚动并点击一个单元格时,这不会打开我的第二个ViewController,我认为这个单元格是一个被禁用的错误索引。

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

2 个答案:

答案 0 :(得分:1)

问题在于这段代码:

SELECT ...

FROM OrderItem

JOIN OrderInformation ON OrderInformation.OrderID = OrderItem.OrderID
JOIN Pizza ON Pizza.PizzaID = OrderItem.PizzaID
JOIN Customer ON Customer.CustomerID = OrderInformation.CustomerID;

因为细胞是可重复使用的。因此,您之前禁用的单元格(例如cell0)将继续被重用。滚动时,例如,cell0将成为cell11。但是,其设置仍处于禁用状态。

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

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

答案 1 :(得分:0)

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