【发布时间】: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;
}
【问题讨论】:
-
BICollectionCell是否覆盖setSelected:或setSelected:animated:。如果是,你会在里面打电话给[super setSelected:selected]还是[super setSelected:selected animated:animated]?见stackoverflow.com/questions/15330844/… -
@Larme 我没有覆盖 bicollection 单元格中的任何东西然后也同样存在
-
添加
cellForRowAtIndexPath的完整代码 -
@Nirav 请检查
标签: ios objective-c uicollectionviewcell