Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning iOS5 Development.pdf
Скачиваний:
7
Добавлен:
09.05.2015
Размер:
15.6 Mб
Скачать

274

CHAPTER 8: Introduction to Table Views

Now, try it again. You’ll see that when you tap the search bar, the index disappears until you’re finished searching.

Adding a Magnifying Glass to the Index

Because we offset the table view’s content, the search bar is not visible when the application first launches, but a quick flick down brings the search bar into view so it can be used. It is also acceptable to put a search bar above the table view, rather than in it, so that the bar is always visible, but this eats up valuable screen real estate. Having the search bar scroll with the table uses the iPhone’s small screen more efficiently, and the user can always get to the search bar quickly by tapping in the status bar at the top of the screen.

The problem is that not everyone knows that tapping in the status bar takes you to the top of the current table. The ideal solution would be to put a magnifying glass at the top of the index the way that the Contacts application does (see Figure 8–33). And guess what? We can actually do just that. iOS includes the ability to place a magnifying glass in a table index. Let’s do that now for our application.

Figure 8–33. The Contacts application has a magnifying glass icon in the index that takes you to the search bar. Prior to iOS 3, this was not available to other applications, but now it is.

www.it-ebooks.info

CHAPTER 8: Introduction to Table Views

275

Only three steps are involved in adding the magnifying glass:

Add a special value to our keys array to indicate that we want the magnifying glass.

Prevent iOS from printing a section header in the table for that special value.

Tell the table to scroll to the top when that item is selected.

Let’s tackle these tasks in order.

Adding the Special Value to the Keys Array

To add the special value to our keys array, all we need to do is add one line of code to the resetSearch method:

- (void)resetSearch {

self.names = [self.allNames mutableDeepCopy]; NSMutableArray *keyArray = [[NSMutableArray alloc] init];

[keyArray addObject:UITableViewIndexSearch];

[keyArray addObjectsFromArray:[[self.allNames allKeys] sortedArrayUsingSelector:@selector(compare:)]];

self.keys = keyArray;

}

Suppressing the Section Header

Now, we need to suppress that value from coming up as a section title. We do that by adding a check in the existing tableView:titleForHeaderInSection: method, and return nil when it asks for the title for the special search section:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { if ([keys count] == 0)

return nil;

NSString *key = [keys objectAtIndex:section]; if (key == UITableViewIndexSearch)

return nil; return key;

}

Telling the Table View What to Do

Finally, we need to tell the table view what to do when the user taps the magnifying glass in the index. When the user taps the magnifying glass, the delegate method tableView:sectionForSectionIndexTitle:atIndex: is called, if it is implemented.

Add this method to the bottom of BIDViewController.m, just above the @end:

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]