2014-03-03 92 views
0

我試圖改變在UICollectionView對象的細胞圖像的狀態更改...細胞的圖像不didDeselectItemAtIndexPath

這是我的代碼的一部分:

-(UICollectionViewCell*) collectionView:(UICollectionView *)collectionView  cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
cell *aCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];  

[aCell.myImage setImage:[UIImage imageWithContentsOfFile:self.imageArray[indexPath.item]]]; 
if (aCell.selected) { 

    aCell.myImage.layer.borderWidth = 1.0; 
    aCell.myImage.layer.borderColor = [UIColor blackColor].CGColor; 
      aCell.mySelect.hidden = NO; 

} else { 

    aCell.myImage.layer.borderWidth = 0.0; 
    aCell.myImage.layer.borderColor = nil; 
      aCell.mySelect.hidden = YES; 
} 

return aCell; 

} 

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 


NSLog(@"Select"); 
cell *selectedCell= [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath]; 

if (selectedCell.selected) { 

    [selectedCell.mySelect setImage:[UIImage imageNamed:@"select.png"]]; 
    selectedCell.myImage.layer.borderWidth = 1.0; 
    selectedCell.myImage.layer.borderColor = [UIColor blackColor].CGColor; 
    selectedCell.mySelect.hidden = NO; 

} else { 
    [selectedCell.mySelect setImage:nil]; 
    selectedCell.myImage.layer.borderWidth = 0.0; 
    selectedCell.myImage.layer.borderColor = nil; 
    selectedCell.mySelect.hidden = YES; } 

} 

當我點擊任何單元格,值選擇更改,但視圖對象不刷新。

+0

'[self.collectionView重裝]'添加? – preetam

回答

0

您需要刷新行選擇與此methos從didSelectItemAtindexPath方法

- (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths

0

爲什麼你出列單元格,選擇單元格時。 寫下面的代碼

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath: (NSIndexPath *)indexPath 
{ 
    NSLog(@"Select"); 
    cell *selectedCell= (cell *)[collectionView cellForItemAtIndexPath:indexPath]; 

    [selectedCell.mySelect setImage:(selectedCell.selected) ? [UIImage imageNamed:@"select.png"]: nil]; 
    [collectionView reloadItemsAtIndexPaths:indexPath]; 
} 
+0

不兼容的指針類型用'UICollectionViewCell *'類型的表達式初始化'cell *'單元格 - 我的類,uicollectionviewcell的子程序 – user3374176

+0

現在檢查它.. –

0

我想你應該覆蓋您的自定義UICollectionViewCellselected/highlighted屬性這一任務,而不是使用重新加載。

0
cell *selectedCell= [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath]; 

此代碼將創建一個NEW單元格!你的viewController不包含這個單元格!

你應該用正確的方法讓選定的單元格:

[yourContentView cellForItemAtIndexPath:indexPath];