![](/img/trans.png)
[英]Deselect default selected cell on selecting any other cell in UICollectionview
[英]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.