2015-01-07 64 views
0

我有一個非常令人沮喪的問題與UICollectionView及其出列機制。UICollectionView單元出隊問題與自定義選擇視圖

簡而言之,我有一個內部帶有標籤的自定義UIView。我在自定義單元格設置此作爲選擇背景圖如下:

//My Custom Cell Class  
- (instancetype)initWithCoder:(NSCoder *)aDecoder{ 
     self = [super initWithCoder:aDecoder]; 

     if(self){ 
      _selectionView = (MyCustomView *)[[[NSBundle mainBundle] loadNibNamed:@"MyNibName" owner:self options:nil] objectAtIndex:0]; 
      self.selectedBackgroundView = _selectionView; 
      [self bringSubviewToFront:_selectionView]; 
     } 
     return self; 
    } 

注意,所有的佈置工作,等,爲細胞和selectedBackgroundView無論是在筆尖完成。

只要選擇單元格,我想設置自定義文本在標籤selectionView所以在我的自定義單元格我也有以下方法:

//In my Custom cell class 
- (void) setSelectedViewLabelText:(NSString *)paramText{ 

    if(!self.isSelected){ 
     return; 
    } 
    self.selectionView.label.text = paramText; 
} 

設置我在我的文本UICollectionViewController:

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

    MyCellClass *selectedCell = (MyCellClass *)[self.collectionView cellForItemAtIndexPath:indexPath]; 
    [selectedCell setSelectedViewLabelText:someString]; 
} 

的問題是,每當UICollectionView回收的電池,它再次inits他們明明setSelectedViewLabelText方法不叫。

我有一個惱人的感覺,我可能不得不跟蹤選定的indexPaths並通過它們枚舉來查看是否選中了一個單元格並調用該方法,但是我有可能存在大量數據集並且可以預見這將如何成爲一個性能問題....任何想法?

在此先感謝!

回答

0

跟蹤您選定的單元格,如:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{  
    MyCellClass *selectedCell = (MyCellClass *)[self.collectionView cellForItemAtIndexPath:indexPath]; 
    [selectedCell setSelectedViewLabelText:someString]; 
    selectedIndexPath = indexPath; 
} 

cellForItemAtIndexPath檢查indexPath:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    //....... 

    if([selectedIndexPath isEqual:indexPath]){ 
     [cell setSelectedViewLabelText:someString]; 
    } 
    return cell; 
} 

希望這有助於.. :)

+0

您好,感謝的是,我可能應該提到我允許在集合視圖中進行多重選擇,所以我擔心的是,爲所選索引路徑的數組執行enumerateObjectsUsingBlock將會達到性能... – jccoder

相關問題