Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable To Deselect UICollectionView Cell once it is reloaded

I have had a string of problems with UIcollectionView and I think the work around is what is currently causing the problem.

Okay, so I have a collectionview that loads objects from a class that updates very frequently, maybe every few seconds.

The first problem I ran into; was that when I selected a cell and highlighted it, another cell lower down on the collection view also became highlighted. My solution was to create an array of selectedcells and Run a loop through the new array to decide what to highlight.

Now, the problem arises when the collectionview is reloaded. The cells continue to stay highlighted and appear selected, but they do no register touches so I am unable to deselect it.

    - (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:        (NSInteger)section
{
    return [self.deviceList count];
}

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DeviceCollectionViewCell *cell = [self.deviceCollectionView dequeueReusableCellWithReuseIdentifier:@"DeviceCell" forIndexPath:indexPath];

    if([self.deviceList count] > indexPath.row)
    {
        cell.device = [self.deviceList objectAtIndex:indexPath.row];
    }

    if ([[self.deviceCollectionView indexPathsForSelectedItems] containsObject:indexPath]) {
        NSLog(@"does it ever?");

       // cell.backgroundColor = [UIColor blueColor];
    }


    for (TDDeviceParser *device in self.selectedDevices)
    {
        if ([cell.device.deviceTextRecord.serialNumber isEqualToString:device.deviceTextRecord.serialNumber])
        {
            [cell setSelected:YES];
            break;
        }
        else
        {
            [cell setSelected:NO];
        }
    }
    return cell;
}

- (UICollectionReusableView *)collectionView: (UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;
    if (kind == UICollectionElementKindSectionHeader) {
        TitleHeaderForDeviceCollectionView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        reusableview = headerView;
    }

    else if (kind == UICollectionElementKindSectionFooter) {
        LogoFooterForDeviceCollectionView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
        reusableview = footerView;
    }
    return reusableview;
}

#pragma mark - UICollectionViewDelegate
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //Iphone Five and Older Ipads: Max2
    int maxCount = 2;
    //ipad 4
    if ([[UIDeviceHardwareVersion platform] isEqualToString:@"iPad3,4"] || [[UIDeviceHardwareVersion platform] isEqualToString:@"iPad3,5"] || [[UIDeviceHardwareVersion platform] isEqualToString:@"iPad3,6"])
    {
        maxCount = 4;
    }
    //Ipod5th,Iphone4 & Iphone4s; Don't select, just play.
    else if ([[UIDeviceHardwareVersion platform] isEqualToString:@"iPod5,1"] ||  [[UIDeviceHardwareVersion platform] isEqualToString:@"iPhone4,1"]  ||  [[UIDeviceHardwareVersion platform] isEqualToString:@"iPhone3,1"]  ||  [[UIDeviceHardwareVersion platform] isEqualToString:@"iPhone3,2"]  ||  [[UIDeviceHardwareVersion platform] isEqualToString:@"iPhone3,3"])
    {
        maxCount = 0;
        //Just play.
        [self.selectedDevices removeAllObjects];
        [self.selectedDevices addObject:[self.deviceList objectAtIndex:indexPath.row]];
        [[TDDeviceManager sharedInstance] removeObserver:self];
        [self performSegueWithIdentifier:@"VideoPlayer" sender:self];
    }

    if (self.selectedDevices.count < maxCount)
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{



    [self.selectedDevices addObject:[self.deviceList objectAtIndex:indexPath.row]];
    [self updatePlayerImages];
    NSLog(@"device Count %d",self.selectedDevices.count);



}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{

    NSLog(@"Should I Deselect?");
    return YES;
}



- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%s",__PRETTY_FUNCTION__);
    //What device do we remove?
    for (TDDeviceParser *device in self.selectedDevices)
    {
        if ([device.deviceTextRecord.serialNumber isEqualToString:((TDDeviceParser*)[self.deviceList objectAtIndex:indexPath.row]).deviceTextRecord.serialNumber])
        {
             NSLog(@"%s Removed %@",__PRETTY_FUNCTION__, device.name);
            [self.selectedDevices removeObject:device];
            break;
        }
    }
     NSLog(@"%s Update %d",__PRETTY_FUNCTION__, self.selectedDevices.count);
    [self updatePlayerImages];
}
like image 348
DTDev Avatar asked Jul 18 '13 18:07

DTDev


1 Answers

I think the problem is that the cell was selected but the CollectionView didn't know that the cell was selected. I added(The Second Line) to fix the problem.

        [cell setSelected:YES];
        [self.deviceCollectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
like image 157
DTDev Avatar answered Nov 02 '22 23:11

DTDev