我使用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;
}
答案 0 :(得分:3)
如果您想要在选择单元格时更改图像,并且如果已选择单元格并且您想要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
方法,现在不需要这样做。