2013-01-02 33 views
1

我在4個單元格中構建了一個具有固定imageView的CollectionView控制器。 我希望imageView更改單元格選擇。 你能幫我解決這個問題嗎?在uicollectionview中更改單元格imageView didSelectItemAtIndexPath方法

謝謝

這裏是我的代碼

的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"); 

}

回答

1

在您的子類中爲UICollectionViewCell實現了KVO偵聽方法。如果你想取消選擇,請確保你將multiselection設置爲true。

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ 
if ([keyPath [email protected]"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

它可以是最愚蠢的回答你的問題,但它爲我工作。我不知道如何使用塞繆爾所說的KEy值路徑。

基本上我發了的NSMutableArray存儲的圖標的狀態,紅色或green..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"]; 

END編輯

+0

感謝名單了很多哥們!儘可能簡單! – dottorfeelgood

+0

歡迎。請投票。它會增加我的聲譽:) – Taseen

相關問題