Prior to iOS7, we added a magnifying glass icon to the top of the UITableView index by prepending UITableViewIndexSearch
to the section index titles.
By dragging to the magnifying glass icon in the section index, the tableView can scroll to the searchBar with the following code:
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
NSInteger resultIndex = [self getSectionForSectionIndex:index];
// if magnifying glass
if (resultIndex == NSNotFound) {
[tableView setContentOffset:CGPointZero animated:NO];
return NSNotFound;
}
else {
return resultIndex;
}
}
However in iOS 7, this would only scroll to the first section instead of the search bar.
To solve this we adjusted the content offset to account for the UITableView's content inset introduced in iOS 7: CGPointMake(0.0, -tableView.contentInset.top)
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
NSInteger resultIndex = [self getSectionForSectionIndex:index];
// if magnifying glass
if (resultIndex == NSNotFound) {
[tableView setContentOffset:CGPointMake(0.0, -tableView.contentInset.top)];
return NSNotFound;
}
else {
return resultIndex;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With