Is there a way to know when the index on uitableview is actually used? I can’t find any methods in the Apple documentation, but I was wondering if anyone else knows anything. I would basically like to make an animation in my table view that changes depending on what section is selected in the index, and I have no idea how to do this without accessing the table view index.
Any help is greatly appreciated.
Thanks.
The
UITableViewDataSourcedelegate method-tableView:sectionForSectionIndexTitle:atIndex:returns anNSIntegerrepresenting 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
NSIntegercalledselectedSectionIndex. 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:In your
-viewWillDisappear:method, unregister the observer:It’s important to do this so that the
-deallocmethod doesn’t throw an exception.Finally, set up the observer method to do something when there is a change to
selectedSectionIndex: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.