Uicollectionview Chess Grid

I need to implement a checkerboard mesh view to show some radial progress bar.! enter image description here

I made a grid using some inefficient algorithm because the UiCollectionView does not use rows and columns and it worked, but when the user scrolled the collection to the limit and the "row" of the collection disappeared from the screen and the user let go of the screen and the UICollectionViewCells reappear , they switched their colors.

I know the switch happens with BOOL, but I don't know how to solve this.

enter image description hereenter image description here

This is my algorithm (I have 2 different UICollectionViewCells, one for each color)

- (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;
}

      

+3


source to share


2 answers


It's more like using a single checkered background internally UICollectionView

, but since I'm not sure if it's doable, you can do something like:

+ (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;
}

      



I don't understand why you are using different reuseIdentifiers and titleLabels for different backgrounds as I don't notice from screens, but you can always edit this code

+2


source


You didn't need to use 2 different cells to change the background. All you need is to set the default background color, and when indexPath.item is even, just set a different cell background color. But it's important to set the default color first, otherwise you will get the same result.



0


source







All Articles