【问题标题】:UICollectionView CustomCell- Not able to deselect already Selected CellUICollectionView CustomCell-无法取消选择已选择的单元格
【发布时间】:2016-07-20 13:38:09
【问题描述】:

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

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

我的代码

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

【问题讨论】:

标签: ios objective-c uicollectionviewcell


【解决方案1】:

如果您想在选择单元格时更改图像并且如果单元格已被选中并且您想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 方法,现在不需要这个了。

【讨论】:

  • 欢迎哥们,快乐编码:)
  • 如果我通过 passign 对象来自另一个视图,如果对象可用,则将其选中。在这种情况下,如果我单击选定的对象,它不会被取消选择
  • 如果我来自另一个 vc 时选择了任何图像,则图像不会改变
  • 能否请您用更多细节和新代码编辑问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-24
  • 2017-03-04
  • 1970-01-01
  • 2019-10-24
相关资源
最近更新 更多