The UITableViewDataSource
delegate method -tableView:sectionForSectionIndexTitle:atIndex:
returns an NSInteger
representing the section selected from the section index. Override this method in whichever class is your data source delegate (probably your table view controller).
Set up a property in the view controller, an NSInteger
called selectedSectionIndex
. Its value is set within the aforementioned delegate method.
Finally, set up an observer in the view controller, which waits for changes to this property and triggers your desired code when a change does get observed.
In your -viewWillAppear:
method, for example:
[self addObserver:self forKeyPath:@"selectedSectionIndex" options:NSKeyValueObservingOptionNew context:nil];
In your -viewWillDisappear:
method, unregister the observer:
[self removeObserver:self forKeyPath:@"selectedSectionIndex"];
It's important to do this so that the -dealloc
method doesn't throw an exception.
Finally, set up the observer method to do something when there is a change to selectedSectionIndex
:
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqual:@"selectedSectionIndex"]) {
// The section index changed, so trigger some cool animation
}
}
The Key-Value Observing pattern is a good, general way to trigger something when an object's value changes somewhere. Apple has written a good "quick-start" document that introduces this topic.