Ios 重新加载UICollectionView单元格后,无法取消选择它

Ios 重新加载UICollectionView单元格后,无法取消选择它,ios,arrays,touch,uicollectionview,reloaddata,Ios,Arrays,Touch,Uicollectionview,Reloaddata,我在UIcollectionView上遇到了一系列问题,我认为解决方法是目前导致问题的原因 [cell setSelected:YES]; [self.deviceCollectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone]; 好的,我有一个collectionview,它从一个更新非常频繁的类加载对象,可

我在UIcollectionView上遇到了一系列问题,我认为解决方法是目前导致问题的原因

        [cell setSelected:YES];
        [self.deviceCollectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
好的,我有一个collectionview,它从一个更新非常频繁的类加载对象,可能每几秒钟更新一次

我遇到的第一个问题;当我选择一个单元格并高亮显示它时,集合视图下方的另一个单元格也高亮显示。我的解决方案是创建一个selectedcells数组,并在新数组中运行一个循环来决定突出显示什么

现在,重新加载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];
}
-(NSInteger)集合视图:(UICollectionView*)视图项编号节:(NSInteger)节
{
返回[自我设备列表计数];
}
-(NSInteger)collectionView中的节数:(UICollectionView*)collectionView
{
返回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];
}
如果([[self.deviceCollectionView indexPathsForSelectedItems]包含对象:indexPath]){
NSLog(@“它曾经吗?”);
//cell.backgroundColor=[UIColor blueColor];
}
用于(自选择设备中的TDDeviceParser*设备)
{
if([cell.device.DeviceExtrecord.serialNumber isEqualToString:device.DeviceExtrecord.serialNumber])
{
[选择的单元格:是];
打破
}
其他的
{
[所选单元格:否];
}
}
返回单元;
}
-(UICollectionReusableView*)collectionView:(UICollectionView*)collectionView视图用于种类的SupplementElementOfKind:(NSString*)种类索引路径:(NSIndexPath*)索引路径
{
UICollectionReusableView*reusableview=nil;
if(种类==UICollectionElementKindSectionHeader){
TitleHeaderForDeviceCollectionView*headerView=[collectionView出列可重用补充视图种类:UICollectionElementKindSectionHeader with ReuseIdentifier:@“headerView”forIndexPath:indexPath];
reusableview=headerView;
}
else if(种类==UICollectionElementKindSectionFooter){
LogoFooterForDeviceCollectionView*footerView=[collectionView出列可重用补充视图种类:UICollectionElementKindSectionFooter with ReuseIdentifier:@“footerView”forIndexPath:indexPath];
reusableview=footerView;
}
返回可重用视图;
}
#pragma标记-UICollectionViewDelegate
-(BOOL)collectionView:(UICollectionView*)collectionView应选择ItemAtIndexPath:(NSIndexPath*)indexPath
{
//Iphone 5及更老的ipad:Max2
int maxCount=2;
//iPad4
如果([[UIDeviceHardwareVersion platform]IseQualString:@“iPad3,4”]|[UIDeviceHardwareVersion platform]IseQualString:@“iPad3,5”]|[UIDeviceHardwareVersion platform]IseQualString:@“iPad3,6”])
{
最大计数=4;
}
//iPod5、Iphone4和Iphone4s;不要选择,只需播放即可。
如果([[UIDeviceHardwareVersion platform]IsequalString:@“iPhone5,1”]|[[UIDeviceHardwareVersion platform]IsequalString:@“iPhone4,1”]|[[UIDeviceHardwareVersion platform]IsequalString:@“iPhone3,1”]|[[UIDeviceHardwareVersion platform]IsequalString:@“iPhone3,2”]|[[UIDeviceHardwareVersion platform]IsequalString:@“iPhone3,3”])
{
最大计数=0;
//玩吧。
[self.selectedDevices removeAllObjects];
[self.selectedDevices addObject:[self.deviceList objectAtIndex:indexPath.row]];
[[TDDeviceManager sharedInstance]移除观察者:self];
[self-PerformsgueWithIdentifier:@“VideoPlayer”发送方:self];
}
如果(self.selectedDevices.count
我认为问题在于单元格被选中,但CollectionView不知道单元格被选中。我添加了(第二行)来解决问题

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

scrollPosition:
采用类型为
UICollectionViewScrollPosition
的值,而不是BOOL。
UICollectionViewScrollPositionNone
是正确的使用值。感谢DTDev提供的解决方案!我遇到了完全相同的问题。