Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone UITableView with UISearchBar and refreshing Section Index

I have a UITableView with a UISearchBar at the top of it.

I also use the Section Index delegate to display ABC...XYZ on the right hand side.

Problem occurs when I click on the Search Box and the keyboard comes up from the bottom, the Section Index squashes so only AD...*Z are displayed - when I close the Search and they keyboard goes away, the Index stays in this "squashed" state until I scroll or similar.

    // animate the searchbar
[UIView beginAnimations:nil context:NULL];
CGRect tempRect = [[self searchBar] frame];
tempRect.size.width = 320-kMarginRight;
[[self searchBar] setFrame:tempRect];
[UIView commitAnimations];  

    // animate the search index back into view
for (UIView *view in [[self tableView] subviews]) {
    if ([view respondsToSelector:@selector(moveIndexIn)]) {
        MovableTableViewIndex *index = (MovableTableViewIndex*)view;
        [index moveIndexIn];
    }
}   

    // try a whole load of stuff to try and get the section indexes to draw again properly
    // none of which work!  

[searchBar setText:@""];
[searchBar resignFirstResponder];

letUserSelectRow = YES;
searching = NO;
[[self navigationItem] setRightBarButtonItem:nil];
[[self tableView] setScrollEnabled:YES];        
[[self tableView] reloadData];
[[self tableView] flashScrollIndicators];
[[self tableView] sizeToFit];
[[self tableView] zoomScale];       
[[self tableView] reloadSectionIndexTitles];

My questions are, has anyone seen this behaviour? Is there a way to redraw the Section Index on the RHS ([[self tableView] reloadData] doesn't do the trick)

Thanks a million!

like image 636
adam Avatar asked May 08 '09 10:05

adam


2 Answers

You should disable the drawing of indexes in when search is activated altogether. Simply return nil in your delegate, something like this:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
  if ([self.searchDisplayController isActive]) return nil;

  ...
}
like image 106
Boon Avatar answered Nov 09 '22 00:11

Boon


You could try using

- (void)flashScrollIndicators

which refreshes the scrollbar and hopefully the section index too?

like image 2
h4xxr Avatar answered Nov 09 '22 01:11

h4xxr