2013-07-18 48 views
4

我有一個UIcollectionView問題的字符串,我認爲解決方法是當前導致問題。無法取消選擇它重新加載UICollectionView單元格

好吧,所以我有一個從一個類更新非常頻繁,也許每隔幾秒鐘加載對象的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]; 
} 

回答

12

我認爲問題在於該單元格被選中,但CollectionView不知道該單元格被選中。我添加了(第二行)來解決這個問題。

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

'scrollPosition:'接受類型爲'UICollectionViewScrollPosition'的值,而不是BOOL。 'UICollectionViewScrollPositionNone'是使用的正確值。 – Travis

+0

感謝DTDev提供的解決方案!我面臨着完全相同的問題。 –

+0

這節省了我的一天 – Pikaurd

相關問題