Under the Bridge

Code: Searching UITableView

Over at iPhoneSDKArticles.com they’ve been running a series on how to work with UITableView. The latest one is definitely worth your attention; it goes into step by step detail on how to manage the entire experience of allowing the user to refine the list with a search field.

When the user begins searching by clicking the search text box, we will do the following:

  1. Set the “searching” variable to YES.
  2. Set the “letUserSelectRow” variable to NO, since we do not want the user to select a row when the search box is empty.
  3. Display a done button on the right bar.
  4. Start searching as the user starts typing, this time allowing the user to select a row.
  5. Use a different data source to bind the table, which display’s the search reults.
  6. Search results are displayed in a single list and they are not grouped.
  7. Hide the keyboard and finish searching, when the user clicks on done.

Source code link doesn’t seem to be working for us at the moment … but most of it’s in the article anyways, and the missing bits can’t be hard to fill in, we’re sure.

h/t: iPhoneKicks!

3
  • Pingback: Code: Searching UITableView | :: superiphoneblog

  • http://www.gabejacobsblog.com/ Gabe Jacobs

    I think this is great, but I would like it to do one more thing.

    Would it be hard to put a UIImage in the detailview and having that image change based on the item selected. I think if you could modify it to do that, would be a great sample app that would teach a lot.

  • http://www.alexcurylo.com/ Alex

    That’s what notifications are for. This set actually goes the other way, to position a list based on change of the image in the detail view, but the idea’s the same.

    When something changes, post a notification, like

    // send out notification so image list we’re from can display
    NSDictionary* info = [NSDictionary dictionaryWithObjectsAndKeys:
    [self.collection collectionController], ICListOwner,
    [NSNumber numberWithInt:collectionIndex], ICImageIndex,
    nil
    ];
    [[NSNotificationCenter defaultCenter] postNotificationName:ICImageIndexChangedNotification object:self userInfo:info];

    and in -viewDidLoad of the view you want to reflect a selection change, listen for it

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(displayedImageChanged:) name:ICImageIndexChangedNotification object:nil];

    – and whenever you call addObserver, don’t forget to removeObserver in dealloc! –

    [[NSNotificationCenter defaultCenter] removeObserver:self];

    then write the function you declared in addObserver to do whatever it is you want. In this particular case I’m scrolling a thumbnail list to reflect that the image in the detail view was changed.

    - (void)displayedImageChanged:(NSNotification*)notification
    {
    UINavigationController* controller = [[notification userInfo] objectForKey:ICListOwner];
    if (self.navigationController != controller)
    return;

    NSNumber* imageIndexNumber = [[notification userInfo] objectForKey:ICImageIndex];
    int imageIndex = [imageIndexNumber intValue];

    int lineToShow = [appDelegate lineForGalleryIndex:imageIndex];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:lineToShow inSection:0];
    [libraryTableView scrollToRowAtIndexPath:indexPath
    atScrollPosition:UITableViewScrollPositionMiddle
    animated:NO
    ];
    }