1

4 つのセルに imageView を固定した CollectionView コントローラーを作成しました。セルの選択時にimageViewを変更したいと思います。これを手伝ってくれませんか?

ありがとうございました

これが私のコードです

CollectionView コントローラ .m

....

-(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;

    int row = [indexPath row];

    image = [UIImage imageNamed:@"ICUbedGREEN.png"];

    myCell.imageView.image = image;

    return myCell;
}

印刷時に画像を変更したいのですが、わかりません...

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:    (NSIndexPath *)indexPath
{
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"ICUbedRED.png"]];

    cell.imageView.image = .....

     NSLog(@"elementselected");

}

4

2 に答える 2

1

それはあなたの質問に対する最も愚かな答えかもしれませんが、それは私にとってはうまくいきました。サミュエルが述べたように、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"];

編集を終了

于 2013-01-03T01:41:24.817 に答える
1

UICollectionViewCell のサブクラスで、KVO リッスン メソッドを実装します。選択を解除したい場合は、必ず multiselection を true に設定してください。

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
 if ([keyPath isEqualToString@"selected"])
 {
      // check value of self.selected to know what you are currently.
      // change picture of image here
 }

}

セルの init メソッドで、自分自身をリスナーとして追加する必要があるため、

 [self addObserver:self forKeyPath:@"selected" options:nil context:nil];
于 2013-01-02T18:59:06.743 に答える