それはあなたの質問に対する最も愚かな答えかもしれませんが、それは私にとってはうまくいきました。サミュエルが述べたように、KEyバリューパスを操作する方法がわかりません。
基本的に、赤または緑のアイコンのステータスを保存するためにNSMutableArrayを作成しました。YESまたはNO。
selState = [[NSMutableArray alloc] initWithObjects:@"NO",@"NO",@"NO",@"NO",nil ];
次に、「ItemForIndexPath」メソッドで、そのアイテムの画像を設定するための値を確認しました
if ([[selState objectAtIndex:indexPath.row] isEqualToString:@"NO"]) {
image = [UIImage imageNamed:@"ICUbedGREEN.png"];
}
else
{
image = [UIImage imageNamed:@"ICUbedRED.jpg"];
}
アイテムが選択されている場合、IndexPathを使用して、NOの値をYESに、またはその逆に変更しました。
if ([[selState objectAtIndex:indexPath.row] isEqualToString:@"NO"]) {
[selState replaceObjectAtIndex:indexPath.row withObject:@"YES"];
}
else if ([[selState objectAtIndex:indexPath.row] isEqualToString:@"YES"]) {
[selState replaceObjectAtIndex:indexPath.row withObject:@"NO"];
}
そして、コレクションビューを更新しました
[self.collectionView reloadData];
ここにすべてのコード
@interface ViewController (){
NSMutableArray *selState;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
selState = [[NSMutableArray alloc] initWithObjects:@"NO",@"NO",@"NO",@"NO",nil ];
}
-(NSInteger)numberOfSectionsInCollectionView:
(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return 4;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Cell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UIImage *image;
if ([[selState objectAtIndex:indexPath.row] isEqualToString:@"NO"]) {
image = [UIImage imageNamed:@"ICUbedGREEN.png"];
}
else
{
image = [UIImage imageNamed:@"ICUbedRED.jpg"];
}
myCell.imageView.image = image;
return myCell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath: (NSIndexPath *)indexPath
{
if ([[selState objectAtIndex:indexPath.row] isEqualToString:@"NO"]) {
[selState replaceObjectAtIndex:indexPath.row withObject:@"YES"];
}
else if ([[selState objectAtIndex:indexPath.row] isEqualToString:@"YES"]) {
[selState replaceObjectAtIndex:indexPath.row withObject:@"NO"];
}
[self.collectionView reloadData];
}
@end
ありがとう
編集---->>
ItemForIndexPathの上記のコードは、次のように記述することもできます。
image = [[selState objectAtIndex:indexPath.row] isEqualToString:@"NO"] ?
[UIImage imageNamed:@"ICUbedGREEN.png"] : [UIImage imageNamed:@"ICUbedRED.jpg"];
編集を終了