簡體   English   中英

UICollectionView CustomCell-無法取消選擇已選擇的單元格

[英]UICollectionView CustomCell- Not able to deselect already Selected Cell

我正在使用collectionCell來選擇和取消選擇collectionCell圖像。 但是當我單擊選定的單元格時,它不會被取消選擇

我按照Nirav建議更改了我的代碼,它適用於當前視圖,但是當我通過傳遞某些對象而來自另一個視圖時,則應將這些對象標記為選中。 如果我單擊選中的標記對象,則不會取消選擇該單元格。

我的密碼

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

BICollectionCell *cell = (BICollectionCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:@"BICollectionCell" forIndexPath:indexPath];

CustVehiclesList *objCustVehiclesList =self.array_VehicleServicesList[indexPath.row];
[cell.labelMake setText:objCustVehiclesList.make];
[cell.lblLicense setText:objCustVehiclesList.licencePlateNo];

if (objCustVehiclesList.vehiclePicture == nil ||[objCustVehiclesList.vehiclePicture isEqualToString:@""])
{
    [cell.imageCarsView setImage:[UIImage imageNamed:@"placeholder.png"]];
}
else
{
    NSString *baseString = [NSString stringWithFormat:@"%@",objCustVehiclesList.vehiclePicture];
    NSData* imageData = [[NSData alloc] initWithBase64EncodedString:baseString options:0];
    UIImage *imageToDisplay = [UIImage imageWithData:imageData];
    [cell.imageCarsView setImage:imageToDisplay];
}

[cell setSelected:YES];
[self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];

if (self.selectedIndexPath == indexPath)
{
    [cell.imgSelectedImage setImage:[UIImage imageNamed:@"vs_tick.png"]];
}
else
{
    [cell.imgSelectedImage setImage:nil];
}

if ([objCustVehiclesList.modelName isEqualToString:self.str_ModelName] && _isCalledFromDetailVC)
{
    [cell.imgSelectedImage setImage:[UIImage imageNamed:@"vs_tick.png"]];
}


if (indexPath.row == self.indexPathToBeSearch.row && self.isCalledFromVehicleVC)
{
    [cell.imgSelectedImage setImage:[UIImage imageNamed:@"vs_tick.png"]];
}


return cell;
}

如果要在選擇單元格時更改圖像,並且如果已經選擇了單元格並且要deselect它,則可以像這樣更改代碼

首先創建一個實例屬性selectedIndexPath像這樣

@property NSIndexPath *selectedIndexPath;

之后,像這樣更改您的cellForItemAtIndexPath

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    BICollectionCell *cell = (BICollectionCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:@"BICollectionCell" forIndexPath:indexPath];
    [cell setSelected:YES];
    [self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
    if (self.selectedIndexPath == indexPath || ([objCustVehiclesList.modelName isEqualToString:self.str_ModelName] && _isCalledFromDetailVC) || (indexPath.row == self.indexPathToBeSearch.row && self.isCalledFromVehicleVC)) {
        [cell.imgSelectedImage setImage:[UIImage imageNamed:@"vs_tick.png"]];
    }
    else {
        [cell.imgSelectedImage setImage:nil];
    }
    <----Label Values--->
    return Cell;
}

現在在didSelectItemAtIndexPath檢查像這樣已經selected單元格

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isCalledFromVehicleVC || self.isCalledFromDetailVC)
    {
     self.isCalledFromVehicleVC = NO;
     self.isCalledFromDetailVC = NO;
    }
    if (self.selectedIndexPath == indexPath) {
        [collectionView deselectItemAtIndexPath:indexPath animated:YES];
        self.selectedIndexPath = nil;
    }
    else {
        self.selectedIndexPath = indexPath;
    }        
    [self.collectionView reloadData];
}

注意-刪除您的didDeselectItemAtIndexPath方法,現在不需要此方法。

暫無
暫無

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

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