我正在使用 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/