I have seven cells having a button each in a collection view.When I click on button1 ,the background of button1 changes to red and all other stay blue(the initial state).If i select button2,it turns red and other turnsn blue.To achive,I have done the following coding:
pragma mark-button selector
-(void)doSomething:(UIButton *) sender {
recipeHeading = (RecipeHeadingCell*)[[sender superview] superview];
NSIndexPath *path = [_headingCollectionView indexPathForCell:recipeHeading];
[_outerCollectionView scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
if(globalTag>=0){
[allValues setObject:[NSNumber numberWithBool:NO] atIndexedSubscript:globalTag];
}
globalTag=path.row;
reloadOn = true;
[allValues setObject:[NSNumber numberWithBool:YES] atIndexedSubscript:globalTag];
dispatch_async(dispatch_get_main_queue(), ^{
[_headingCollectionView reloadData];
});
}
pragma mark-collectionview datasource
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
if(collectionView==_headingCollectionView){
recipeHeading = (RecipeHeadingCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"headingCell" forIndexPath:indexPath];
_headingCollectionView.delegate = self;
[recipeHeading.headingBtn setTitle:[recipeTypeArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
[recipeHeading.headingBtn addTarget:self action:@selector(doSomething:) forControlEvents:UIControlEventTouchUpInside];
bool selected = [allValues[indexPath.row] boolValue];
if(selected)
{recipeHeading.headingBtn.backgroundColor = [UIColor redColor];
}
else
{
recipeHeading.headingBtn.backgroundColor = [UIColor blueColor];
}
return recipeHeading;
}
else if (collectionView==_outerCollectionView){
RecipeOuterCell *outerCell =(RecipeOuterCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"outerCell" forIndexPath:indexPath];
collectionView.pagingEnabled = true;
return outerCell;
}
else{
static NSString *simpleTableIdentifier = @"innerCell";
RecipeInnerCell *innerCell = (RecipeInnerCell*)[collectionView dequeueReusableCellWithReuseIdentifier:simpleTableIdentifier forIndexPath:indexPath];
// collectionView.pagingEnabled = true;
[innerCell populateRecipeScreen:responseArray index:indexPath];
return innerCell;
}
}
Below is my problem: On click of button, when heading collection view is reloaded and background colour of buttons change ,it gives a flickering effect i.e. it first shows the title for other indexpath.row for a nanosecond and then shows the correct title.I am not able the get the exact line causing problem .Please give your suggestions and help me to overcome this problem.Any kind or help would be appreciated.Thanks in advance!