iosobjective-cuicollectionviewuicollectionviewcelluicollectionviewdelegate

CollectionView Default Item Selection


I have implemented one row-multiple column collectionview which stays at the top of the HomeViewController.

Initially, I want to set a default item as being selected and highlighted with a red line color beneath and also as well as text color red, refer to the image as follows.

For some reason(s), it shows multiple category items have red line colors beneath which is not what I expected, but text colors are correct. I do not know what I am doing wrong?

enter image description here

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

HomeViewController.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];
            }
        });
}

Solution

  • You didn't post all relevant code here. From your code there may be reason :

    1) Your highlighted label color is set to default red in cell. When you pass your code changes that cell only.

    For this set your highlighted label background color clear in xib/storyboard.