Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView section index not able to scroll to search bar index

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.

like image 679
Min Tsai Avatar asked Sep 30 '13 11:09

Min Tsai


1 Answers

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;
    }
}
like image 169
Min Tsai Avatar answered Oct 26 '22 18:10

Min Tsai