Monika Patel Monika Patel - 5 months ago 18
iOS Question

How to identify specific cell in UICollectionview

enter image description here

I want set color black color for cell1,cell4,cell7 ,...n & white color for cell2,cell5,cell8 ,...n
Note: Number of cell are not fixed.

How can I implement this??

Answer

I assume that your cellForItemAtIndexPath knows how to get cell number (i.e. 1, 2, 3, 4 on your picture) from NSIndexPath passed to the method. Then you can decide on what color to use by obtaining a remainder of division by 3, like this:

// Decode index path into cell number from your picture
int cellNumber = [self getCellNumberFromIndexPath:indexPath];
// Figure out the color
if (cellNumber % 3 == 1) {
    // 1, 4, 7, etc
    ... // Use black color
} else if (cellNumber % 3 == 2) {
    // 2, 5, 8, etc
    ... // Use white color
}