ios - UICollectionView CustomCell - 无法取消选择已经选定的单元格

标签 ios objective-c uicollectionviewcell

我正在使用 collectionCell 选择和取消选择 collectionCell 上的图像。 但是当我点击选中的单元格时,它不会被取消选中

我根据 Nirav 建议更改了我的代码,它适用于当前 View ,但是当我通过传递某个对象来自另一个 View 时,这些对象应该被标记为选中。如果我单击选中的标记对象,它不会取消选中单元格。

我的代码

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

最佳答案

如果你想在选择单元格时更改图像,并且如果单元格已经被选中并且你想取消选择它,那么你可以像这样更改你的代码

首先像这样创建一个实例属性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 中检查已经选中的单元格,像这样

-(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 方法,现在不需要这个了。

关于ios - UICollectionView CustomCell - 无法取消选择已经选定的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38482920/

相关文章:

ios - 适用于 iOS 3.2 的 iPad 模拟器未运行

ios - ngx-translate 不适用于 iOS

iphone - Objective C 中的部分模糊 UIImage

iOS:如何选择 UITabBar 中的特定选项卡和该选项卡中的特定行

swift - 如何保存数据并返回到 Collection View ?

ios - View Controller 中的嵌入式 UICollectionView 未被调用 Swift

ios - UICollectionViewCell 宽度大于 UICollectionView 错误处理

ios - 在 iOS 应用程序中检测 AirPlay 镜像

javascript - 如何在 iOS Safari 上使用 JavaScript 下载 JSON 文件

ios - UIView 摆动