簡體   English   中英

單元格的圖像在didDeselectItemAtIndexPath中未更改

[英]Image of cell does not change in didDeselectItemAtIndexPath

我嘗試在UICollectionView對象的單元格中更改圖像的狀態...

這是我的代碼的一部分:

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

}

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

您需要從didSelectItemAtindexPath方法刷新使用此方法選擇的行

- (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths

選擇單元格時為什么要使單元出隊。 編寫以下代碼

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

我認為您應該為此任務重寫自定義UICollectionViewCell子類中的selected/highlighted屬性,而不是使用reload。

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

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

您應該使用正確的方法來獲取選定的單元格:

[yourContentView cellForItemAtIndexPath:indexPath];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM