2014-10-02 102 views
3

我必須實現國際象棋風格網格視圖來顯示一些徑向進度條。 enter image description hereUicollectionview國際象棋風格網格

我使用一些低效的算法來做到這一點,因爲UiCollectionView不使用的行和列進行的網格,它的工作,但是當用戶滾動收集到了極限和收集的「行」從屏幕消失並且用戶釋放屏幕並且UICollectionViewCells再次出現,他們切換其顏色。

我知道BOOL切換髮生了,但我不知道如何解決這個問題。

enter image description here enter image description here

這是我的算法(我有2個型動物UICollectionViewCells,每一種顏色)

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *identifier = @"Cell"; 
    static NSString *identifierPair = @"CellPair"; 
    UICollectionViewCell *cell ; 
    UILabel *title; 

    //...Creating the circle progress bar.... 


    if ((indexPath.item % 2) == 0) { 
     if (isPair) { 
      cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifierPair forIndexPath:indexPath]; 
      isPair = NO; 
      title = (UILabel *)[cell viewWithTag:12]; 
     } 
     else 
     { 
      cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 
      isPair = YES; 
      title = (UILabel *)[cell viewWithTag:11]; 

     } 

    } 
    else { 
     if (isUneven) { 
      cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifierPair forIndexPath:indexPath]; 
      isUneven = NO; 
      title = (UILabel *)[cell viewWithTag:12]; 
     } 
     else 
     { 
      cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 
      isUneven = YES; 
      title = (UILabel *)[cell viewWithTag:11]; 
     } 
    } 


    //...Setting the name to the cell.... 

    return cell; 
} 
+0

計數是否改變?當他們達到100%時你是否將其移除?當一行被添加或刪除時,你是否全部更新了它們?難道你不想只是總是在光明與黑暗之間交替(例如,即使是光明與奇怪的黑暗)? – 2014-10-02 17:27:08

+0

這個圓圈是靜態的(我用它只是爲了向用戶顯示信息),它們不會被用戶刪除或添加 – 2014-10-02 17:40:28

回答

2

這似乎使用UICollectionView內單格仔背景的更多的情況,但因爲我不知道它是可行的,你可以這樣做:

+ (BOOL)checkersCellAtIndexIsDark:(NSIndexPath *)indexPath { 
    NSInteger squareIndex = indexPath.item % 4; 
    return (squareIndex == 0 || squareIndex == 3); 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; 
    cell.backgroundColor = ([[self class] checkersCellAtIndexIsDark:indexPath])? [UIColor orangeColor] : [UIColor yellowColor]; 

    // Set other things 

    return cell; 
} 

我不明白你爲什麼使用不同的reuseIdentifiers和titleLabels爲不同的背景,因爲我沒有注意到從屏幕上,但你可以隨時編輯此代碼

+0

直到現在我還沒有看到它,讓4個部門解決這一切。非常感謝! – 2014-10-06 21:45:10

+0

我正在使用不同的重用標識符只是爲了絕望!,我刪除了原型單元格之一。 – 2014-10-06 21:47:41

0

您沒有使用2個不同的細胞改變背景。 您只需設置背景的默認顏色,並在indexPath.item爲偶數時,爲單元格背景設置其他顏色。 但首先設置默認顏色很重要,否則您將獲得相同的結果。

+0

如果我只是在indexPath.item時更改背景,我甚至會有一列完整的米色另一列棕色...因爲收集工作是這樣的: '0 1 6 7'。 – 2014-10-06 13:56:15