我有7个单元格,每个单元格都有一个按钮。当我点击button1时,button1的背景变为红色,其他所有变为蓝色(初始状态)。如果我选择button2,它会变为红色等变成了蓝色。为了实现,我做了以下编码:
-(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];
});
}
-(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;
}
}
以下是我的问题: 单击按钮,当重新加载标题集合视图并且按钮的背景颜色发生变化时,它会产生闪烁效果,即它首先显示其他indexpath.row的标题为纳秒,然后显示正确的标题。我无法获得导致问题的确切行。请提出你的建议,并帮助我克服这个问题。任何善意或帮助将不胜感激。谢谢你提前!