2014-03-26 50 views
0

我想調用我的方法,突出顯示UICollectionViewCells我有..一切工作正常,但當UIView第一次加載我試圖選擇集合視圖中的第一個項目像這樣什麼都沒有發生時,我打電話CollectionView:didSelectItemAtIndexPath

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


} 

我讀過,也許你可以調用這個方法太早?我不確定這是否是這種情況,但我不知道如何測試它,或者如果我使用的代碼甚至是正確的。

任何幫助,將不勝感激。

+0

對此表示感謝。 – HurkNburkS

回答

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]; 
     }];  
} 
+0

nope thats not it ..我試着調試一個斷點和方法是從來沒有被稱爲..所以,即使它確實使它到目前爲止它不無關係 – HurkNburkS

+0

好吧抱歉。我沒有看到第一行代碼。當你編程調用didSelectItemAtIndexPath它不會觸發委託方法。只有當用戶點擊它時纔會調用它。如果您想在選中單元格時執行此動畫。你必須重寫uitableviewcell。生病嘗試張貼一些代碼爲我原來的答案。 – nsuinteger

+0

好吧,所以我有一個subclassed的collectionviewcell我會添加這個,然後我只是打電話[cell setSelected:YES];在collectionview類 – HurkNburkS

1

你可能會調用選擇爲時尚早。你不會說你從哪裏調用它(除了「當UIView第一次加載時」),但如果你調用它比viewDidAppear更早,集合視圖將不會加載任何單元格,所以你會什麼也不選。

您可以用斷點確認 - 將一個放在您選擇單元格的代碼中,另一個放入cellForItem方法中。如果選中的一個先被擊中,則知道單元格尚未加載。

相關問題