문제

I am trying to call my method that highlights the UICollectionViewCells I have.. everything works normally but when the UIView first loads I am trying to select the first item in the collection view like this

[photoCollectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionNone];

This is what my selection method looks like, that works well when selecting photos.

#pragma mark -- UICollectionView Delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    oldCell = currentCell;
    currentCell = indexPath;

    // animate the cell user tapped on
    UICollectionViewCell  *cell = [collectionView cellForItemAtIndexPath:indexPath];
    UICollectionViewCell *oCell = [collectionView cellForItemAtIndexPath:oldCell];

    NSDictionary *currentSelectedDict = [imageArray objectAtIndex:indexPath.row];
    UIImage *updatePreviewImage = [UIImage imageWithData:[currentSelectedDict objectForKey:@"DImage"]];
    [previewImageView setImage:updatePreviewImage];
    previewImageView.userInteractionEnabled = YES;

    typeTextF.text = [currentSelectedDict objectForKey:@"DocumentType"];
    descriptionText.text = [currentSelectedDict objectForKey:@"DocumentDescription"];
    dateLabel.text = [NSString stringWithFormat:@"%@", [currentSelectedDict objectForKey:@"DateString"]];


    [UIView animateWithDuration:0.2
                          delay:0
                        options:(UIViewAnimationOptionAllowUserInteraction)
                     animations:^{
                         NSLog(@"animation start");
                         [cell setBackgroundColor:[UIColor whiteColor]];
                         [oCell setBackgroundColor:[UIColor clearColor]];
                     }
                     completion:^(BOOL finished){
                         NSLog(@"animation end");
                         [cell setBackgroundColor:[UIColor whiteColor]];
                         [oCell setBackgroundColor:[UIColor clearColor]];
                     }
     ];


}

I have read that maybe you can call this method too early? I am not sure if that's the case but I don't know how to test it, or if the code I am using is even correct.

도움이 되었습니까?

해결책

try this instead. Override UICollectionViewCell class & add this method

- (void)setSelected:(BOOL)selected
{
    [super setSelected:selected];

        [UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{

            self.contentView.backgroundColor = [UIColor redColor];
        } completion:^(BOOL finished) {
            self.contentView.backgroundColor = [UIColor greenColor];
        }];     
}

다른 팁

You're probably calling the selection too early. You don't say where you are calling it from (except for "when the UIView first loads") but if you're calling it any earlier than viewDidAppear, the collection view will not have loaded any cells, and so you'll be selecting nothing.

You can confirm this with breakpoints - put one on the code where you select the cell, and one in your cellForItem method. If the selection one is hit first, you know the cells haven't been loaded.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top