ios - 重新加载后无法取消选择 UICollectionView 单元格

标签 ios arrays touch uicollectionview reloaddata

我在使用 UIcollectionView 时遇到了一系列问题,我认为解决方法是当前导致问题的原因。

好的,所以我有一个 collectionview,它从一个更新非常频繁的类中加载对象,可能每隔几秒更新一次。

我遇到的第一个问题;是当我选择一个单元格并将其突出显示时, Collection View 下方的另一个单元格也突出显示。我的解决方案是创建一个选定单元格数组,然后在新数组中运行一个循环来决定要突出显示的内容。

现在,问题出现在重新加载 collectionview 时。这些单元格继续保持突出显示并显示为选中状态,但它们没有记录触摸,因此我无法取消选择它。

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

最佳答案

我认为问题在于单元格已被选中,但 CollectionView 并不知道单元格已被选中。我添加了(第二行)来解决问题。

        [cell setSelected:YES];
        [self.deviceCollectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];

关于ios - 重新加载后无法取消选择 UICollectionView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17731551/

相关文章:

ios - 如何使用一个 ContainerView 显示多个 ViewController

ios - Swift - Firebase - 身份验证电子邮件无密码

c - 实现 Malloc : case for splitting a block

ios - CJSON反序列化错误

ios - Objective-C 中的自定义构造函数

java - 如何在 libGDX 中测量触摸压力

iphone - TouchesBegan - 只需要最后一次触摸操作 - iPhone

java - 在 Java 中同步比较两个数组

javascript - Javascript Map 函数是异步的吗?