当我调用CollectionView:didSelectItemAtIndexPath时,什么也没发生

问题描述 投票:0回答:2

我试图调用我的方法突出显示我有的UICollectionViewCells ..一切正常但是当UIView第一次加载时我试图选择集合视图中的第一个项目,如下所示

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

这就是我的选择方法,在选择照片时效果很好。

#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]];
                     }
     ];


}

我读过,也许你可以过早地调用这种方法?我不确定是不是这样,但我不知道如何测试它,或者我使用的代码是否正确。

ios objective-c uicollectionview uicollectionviewcell
2个回答
2
投票

试试这个。重写UICollectionViewCell类并添加此方法

- (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];
        }];     
}

1
投票

你可能过早地调用选择。你没有说出你在哪里调用它(除了“当UIView第一次加载时”),但如果你在viewDidAppear之前调用它,那么集合视图将不会加载任何单元格,所以你将成为什么都不做。

您可以使用断点进行确认 - 在您选择单元格的代码中放置一个,在您的cellForItem方法中放置一个。如果首先点击选择一个,则表示尚未加载单元格。

© www.soinside.com 2019 - 2024. All rights reserved.