Assume you have a table view filled with objects. Assume you have sections for these objects, and you have setup the section indexes. Until this point, nothing which cannot be found in SDK’s documentation.
Assume now you want to add a search bar to your table view. What do you do? To answer this question, we will start from source code available to all of us, implementing the required table view with section indexes, we shall use the TableViewSuite’s 3rd example, I have named “3_SimpleIndexedTableView” (you can download the tutorial package there).
From this base, I will indicate the updates necessary to display a search bar in the first row. I will do this the fast way, so it may not be really nice. And I will not implement everything required to do search and display search results, you will have to look at documentation for this (this will essentially consist in connecting the search bar with required objects and delegates, providing content for search results).
So, what do we do to the TableViewSuite’s 3rd example:
- We change the numberOfSectionsInTableView: method in order to return one row more (you guess? …the row for the search bar naturally!):
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// The number of sections is the same as the number of titles in the collation.
return [[collation sectionTitles] count] + 1; // CHANGE: add one for the search cell
}
- We update (a little) the tableView:numberOfRowsInSection: and tableView:titleForHeaderInSection: methods in order to have it return good number of rows for the good sections:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) return 1; // CHANGE: return one for the search cell
// The number of time zones in the section is the count of the array associated with the section in the sections array.
NSArray *timeZonesInSection = [sectionsArray objectAtIndex:section - 1];
return [timeZonesInSection count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) return nil;
return [[collation sectionTitles] objectAtIndex:section - 1];
}
- Finally, we update the most important method, I have named tableView:cellForRowAtIndexPath: :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// CHANGE: add the search cell
if ([indexPath indexAtPosition:0] == 0) {
static NSString *CellIdentifier = @"SearchCell";
UITableViewCell *searchBarCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[searchBarCell addSubview:[[UISearchBar alloc] initWithFrame:searchBarCell.frame]];
return searchBarCell;
}
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Get the time zone from the array associated with the section index in the sections array.
NSArray *timeZonesInSection = [sectionsArray objectAtIndex:indexPath.section - 1];
// Configure the cell with the time zone's name.
TimeZoneWrapper *timeZone = [timeZonesInSection objectAtIndex:indexPath.row];
cell.textLabel.text = timeZone.localeName;
return cell;
}
- Like I told before, you should then get the searchBarCell connected to the good objects to have it up and working. One last tip, I use the UISearchDisplayController class and connect them both in this way:
UISearchDisplayController *sdc = [[UISearchDisplayController alloc] initWithSearchBar:sb contentsController:self];
[sdc setValue:self forKey:@"searchResultsDataSource"];
[sdc setValue:self forKey:@"searchResultsDelegate"];
Uh oh, I almost forgot to give you the best tip! How to have the “search” tool icon (what’s this tool’s name in English?) in your section indexes? Just follow the guide… (’cause this cannot be found in the doc, but you may find it elsewhere on the net, like I did – however it’s too long ago, I have not kept the source link).
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [[NSArray arrayWithObject:@"{search}"] arrayByAddingObjectsFromArray:[collation sectionIndexTitles]];
}
If you have a problem, you can’t make it work in your code, do not hesitate to ask… I have tried this code changes and it worked for me, but I may have forget something!
[Edited] As requested, please find the xCode project there.
Like this:
Be the first to like this post.