0

I am trying to add observer (KVO) to observe my custom cell. Once the cell is selected I should receive a notification of the event. My code:

[colMain addObserver:self forKeyPath:@"colMain" options:0 context:NULL];

}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{

    if (keyPath == @"colMain") {
        NSLog(@"cell Selected");
        [self performSelector:@selector(deleteCell) withObject:nil];

    }
}

colMain stands for collectionView. I am not quite sure how to do it cause I don't have customCell as a property otherwise it does not compile. Any ideas?

uml
  • 1,171
  • 4
  • 16
  • 28

1 Answers1

3

Why not just set a delegate on your collection view and then implement one of these two methods?

[– collectionView:shouldSelectItemAtIndexPath:]

[– collectionView:didSelectItemAtIndexPath:]

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • I am too amateur for such technique. I want simply use KVO. – uml Feb 04 '13 at 00:57
  • Don't be scared of delegates. Once you get accustomed to the concept, delegate protocols can be your best (Objective C) friend. Setting it up can be as easy as a "`[colMain setDelegate: self]`" in your view controller's "`viewDidLoad:`" method. – Michael Dautermann Feb 04 '13 at 01:00
  • Some more details regarding that cause I am very beginner in Objective C, please. – uml Feb 04 '13 at 02:01
  • Well, as I mentioned, simply set your view controller to be the delegate of your collection view and then you must implement one of those methods above. When the item is selected, that method will be called via the delegate protocol. Try it and see what happens! – Michael Dautermann Feb 04 '13 at 02:52