How to determine when an index letter of a UITableView section is being processed?

On iOS, is there a delegate method that gets called when listening to the index of a table view's section? For example, if the letters AZ and B are in the index of the table partition, how can I determine which letter the user was listening on? I looked through all the documentation but didn't find anything.

+3


source to share


2 answers


There is no delegate that allows you to define when the section index letter is used. However, you can still do it pretty easily.

If we use UIScrollViewDelegate

scrollViewDidScroll

breakpoints to set, we can get closer to when the user closes the index and see what kind of messaging we could use. Here's a look at the frames immediately after the user closes 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 it UITableView

has a private method _sectionIndexChanged

. Also, after changing the index, the internal call UITableView

calls setContentOffset

. Any of these can be used to provide a mechanism for determining when a section index is indexed.

If I was doing this, I would prefer to subclass to UITableView

intercept _sectionIndexChanged

and extend the protocol UITableViewDelegate

to provide the required messages. Alternatively, you can subclass UITableView

and intercept setContentOffset

, but that would take care of corner cases when setContentOffset

called without hitting the index.

+1


source


I think there are two sensible solutions that might work for your purpose.

First, you can override the delegate method scrollViewDidScroll:

to show and update the HUD if the offset has changed. Of course, this will make it appear when the user scrolls without an index. You can prevent most of these cases by simply disabling the display of the HUD when the scroll properties are dragging

or decelerating

are YES. There will still be cases where the HUD appears (e.g. scrollToTop, resize), but that might be acceptable to you.



Another option is to just create your own index control. It wouldn't be a lot of work to lay out multiple shortcuts inside a custom view on the right side of your table view using multiple gesture recognizers. Then you can scroll through the table view and display whatever you want.

+1


source







All Articles