在uicollectionview didSelectItemAtIndexPath方法中更改单元格imageView

时间:2013-01-02 11:01:39

标签: uiimageview uicollectionview uicollectionviewcell

我在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");

}

2 个答案:

答案 0 :(得分: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];

答案 1 :(得分:1)

对你的问题可能是最愚蠢的答案,但它对我有用。我不知道如何使用Samuel所述的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"];
}

然后更新了集合View

[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"];

结束编辑

相关问题