2014-05-07 27 views
0

enter image description heredequeueReusableCellWithReuseIdentifier上的CollectionView滾動後出了錯

我有這個問題,一個UICollectionViewCell內UICollectionView: 最後UICollectionViewCell沒有得到填充13 - 18,而是第一個GET的複製。 我知道它與「dequeueReusableCellWithReuseIdentifier」有關,但如果我不對下面的標籤進行檢查,我的整個集合就會變得混亂。

我一直在努力解決這個問題,並搜索了大約一天,但我找不到如何解決這個問題。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *[email protected]"pageCell"; 
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; 
    int no = indexPath.item; 
    if(collectionView == _pageCollectionView) 
    { 
     //Collection pages 
     cell.tag=no+1; 
     UICollectionViewFlowLayout *layoutMods=[[UICollectionViewFlowLayout alloc] init]; 
     [layoutMods setSectionInset:UIEdgeInsetsMake(0, 0, 0, 0)]; 
     [layoutMods setMinimumInteritemSpacing:0]; 
     [layoutMods setMinimumLineSpacing:0]; 
     [layoutMods setItemSize:CGSizeMake(125, 125)]; 
     _modsCollectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0,0, _pageCollectionView.frame.size.width, _pageCollectionView.frame.size.height) collectionViewLayout:layoutMods]; 
     [_modsCollectionView setDataSource:self]; 
     [_modsCollectionView setDelegate:self]; 
     _modsCollectionView.pagingEnabled = YES; 
     [_modsCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"pageCell"]; 

     NSMutableArray *randColor = [[NSMutableArray alloc] init]; 
     for(int i=0;i<3;i++) 
     { 
      int lowerBound = 0; 
      int upperBound = 255; 
      NSNumber *rndValue = [NSNumber numberWithFloat:(lowerBound + arc4random() % (upperBound - lowerBound))/255.0]; 
      [randColor insertObject:rndValue atIndex:i]; 
     } 

     float r = [[randColor objectAtIndex:0] floatValue]; 
     float g = [[randColor objectAtIndex:1] floatValue]; 
     float b = [[randColor objectAtIndex:2] floatValue]; 
     _modsCollectionView.backgroundColor = [UIColor colorWithRed:r green:g blue:b alpha:1]; 
     [cell addSubview:_modsCollectionView]; 
    } 
    else 
    { 
     if(cell.tag == 0) 
     { 
      cell.tag = 2; 

      NSMutableDictionary *mod=[modules objectAtIndex:modulePointer]; 

      cell.backgroundColor=[mod objectForKey:@"color"]; 

      NSString *slug = [mod objectForKey:@"slug"]; 

      NSString *imgName=[NSString stringWithFormat:slug,@".png"]; 
      UIImageView *customBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imgName]]; 
      customBackground.frame = CGRectMake(0,0, 125, 125); 
      [cell addSubview:customBackground]; 
      customBackground.contentMode = UIViewContentModeScaleAspectFit; 
      customBackground.clipsToBounds = YES; 
      modulePointer++; 
     } 
    } 
    return cell; 
} 

我在做什麼錯?

+0

可能與您的具體問題無關,但在'cellForItemAtIndexPath:'內部分配'_modsCollectionView'等實例變量很奇怪。 – Jkmn

+0

我知道它看起來很奇怪,但它似乎根據我想要的圖像工作。我已經嘗試過使用頁面控制器,但是如果沒有.xib文件,它就無法工作,我想阻止在.xib或storyboard文件中設計不同的屏幕。 如果任何人有任何關於如何在圖像中達到目標的想法,即使沒有collectionView,也沒關係。 –

回答

1

Got it!我不得不使用- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath中的視圖標籤來製作一些魔術。

if(collectionView == _pageCollectionView)我把:

for(id subview in [cell subviews]) 
{ 
     [subview removeFromSuperview]; 
} 
cell.tag=itemsPerScreen*no; 

然後在下面的else

int thisIndex=[collectionView superview].tag+no; 
NSMutableDictionary *mod=[modules objectAtIndex:thisIndex]; 
0

您需要創建自己的自定義UICollectionViewCell,以防止這種情況發生。

相關問題