我已经实现了一个行多列的集合视图,它位于HomeViewController
.
最初,我想将默认项目设置为被选中并在下方用红线颜色突出显示,以及文本颜色为红色,请参考如下图像。
出于某种原因,它显示多个类别项目有红线颜色,这不是我所期望的,但文本颜色是正确的。我不知道我做错了什么?
CategoryCollectionViewCell.m
@implementation CategoryCollectionViewCell
@synthesize categoryLabel,highlightedLabel;
- (void)setSelected:(BOOL)selected
{
if(selected)
{
self.categoryLabel.textColor = [UIColor redColor];
self.highlightedLabel.backgroundColor = [UIColor redColor];
}
else
{
self.categoryLabel.textColor = [UIColor lightGrayColor];
self.highlightedLabel.backgroundColor = [UIColor clearColor];
}
}
@end
主视图控制器.m
-(void) viewWillAppear: (BOOL) animated {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^(void) {
[self loadFromURL]; // that is not related with categoryCollectionView
dispatch_async(dispatch_get_main_queue(), ^{
[self.categoryCollectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];
}
});
}