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

时间:2014-03-26 07:08:57

标签: ios objective-c uicollectionview uicollectionviewcell

我正在尝试调用我的方法突出显示我所拥有的UICollectionViewCell ..一切正常但是当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]];
                     }
     ];


}

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

2 个答案:

答案 0 :(得分: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 :(得分:1)

你可能过早地调用选择。你没有说出你在哪里打电话(除了#34;当UIView第一次加载时#34;)但是如果你在viewDidAppear之前调用它,那么集合视图将不会被加载任何细胞,所以你什么也不做。

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