Reputation: 713
In iOS, is there a delegate method that is called when the section index of a table view is tapped? For instance, if the table's section index lists the letters A-Z and B is tapped, how can I determine what letter the user tapped? I've looked throughout the documentation but am unable to find anything.
Upvotes: 3
Views: 487
Reputation: 40492
There is no delegate which allows you do determine when a section index letter is tapped. However, you can still do so fairly easily.
If we use the UIScrollViewDelegate
scrollViewDidScroll
to set a breakpoint, we can get pretty close to when the user taps the index and see what messaging we might be able to take advantage of. Here's a look at the frames directly after the user taps the index:
frame #1: UIKit`-[UIScrollView(UIScrollViewInternal) _notifyDidScroll] + 56
frame #2: UIKit`-[UIScrollView setContentOffset:] + 645
frame #3: UIKit`-[UITableView setContentOffset:] + 362
frame #4: UIKit`-[UIScrollView(UIScrollViewInternal) _setContentOffset:animated:animationCurve:animationAdjustsForContentOffsetDelta:] + 669
frame #5: UIKit`-[UITableView _sectionIndexChangedToIndex:title:] + 285
frame #6: UIKit`-[UITableView _sectionIndexChanged:] + 189
So, UITableView
has a private method _sectionIndexChanged
. In addition after the index has changed, the UITableView
internals call setContentOffset
. Either of these could be used to provide a mechanism to determine when a section index is tapped.
If I were doing this I would likely subclass UITableView
to intercept _sectionIndexChanged
and extend the UITableViewDelegate
protocol to provide the needed messages. Alternately, you could subclass UITableView
and intercept setContentOffset
, but that would require taking care of corner cases where setContentOffset
is called without the index being tapped.
Upvotes: 1
Reputation: 57168
I think there are two reasonable solutions which could work for your goal.
First, you could override the delegate method scrollViewDidScroll:
to show and update the HUD if the offset has changed. Of course, this will cause it to appear when the user scrolls without the index. You can prevent the majority of these cases by simply disabling the display of the HUD when the scroll views's dragging
or decelerating
property is YES. There will still be some cases where the HUD will appear (e.g. scrollToTop, resizing) but this might be acceptable to you.
The other option is to just build your own index control. It really wouldn't be a lot of work to lay out a few labels inside a custom view along the right-hand side of your table view with a couple of gesture recognizers. Then you can scroll the table view and display whatever HUD you want.
Upvotes: 1