ORStudios
ORStudios

Reputation: 3233

How To Change UICollectionView Cell Background Colour From Outside UICollectionView

I have a pie chart that is clickable, each section represents a cell inside the UICollectionView. On clicking a pie section I want to change the background colour of the cell it corresponds to. I am using the following but nothing is happening.

- (void)pieChart:(XYPieChart *)pieChart didSelectSliceAtIndex:(NSUInteger)index {

NSIndexPath *path = [NSIndexPath indexPathWithIndex:index];

[[self.collectionCategories cellForItemAtIndexPath:path] setBackgroundColor:[UIColor blueColor]];

}

Any ideas, Thanks.

Upvotes: 0

Views: 180

Answers (1)

rdelmar
rdelmar

Reputation: 104082

I think the problem is the way you create your index path. You should be doing it like so (assuming that you have only one section),

 NSIndexPath *path = [NSIndexPath indexPathForItem:index inSection:0];

The indexPath for a table view or collection view needs to specify both the section and the row. If you log the index path created by the above method, you will see that it's length is 2, whereas the way you originally did it, the length is only 1.

Upvotes: 1

Related Questions