Tableview |
Modified: |

Disclosure
Example from The iPhone Developer's Cookbook by Erica Sadun, Addison Wesley ISBN-13 978-0-321-55545-8
Downloads
Search Table application
Overview
UITableViewController provides a table view and navigation. Note that a UITableViewController does not hold the data but only presents the data.
The example displays colors in a scrollable table and provides user search on partial matches to produce a subset of the complete color list.
A table is typically filled from an array, in the example, a Model object manages this array. User input into the search field is passed to the Model which returns an array filled with partial matches.
The ViewController manages the view but also creates view programmatically. The view is primarily a table that displays the contents of the Model data. Table entries can also be selected by tapping, in this case the color of the navigation bar is changed to that of the selected row entry.
UITableViewController
Subclassing UITableViewController requires methods to be over-ridden:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView returns the number of sections in the table.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section returns the number of rows in section number; for the example, the size of the array filling the table. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath returns the cell at the designated table row when only one section. Old cells are recycled when available, faster than creating new ones.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath called when user taps a row.
Creating the search bar requires the following:
search = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 280.0f, 44.0f)]; create an instance.
search.delegate = self; setting the delegate to receive notifications of changes to the sear
ch bar.
self.navigationItem.titleView = search; adding the search bar to the UITableViewController navigation. Delegate methods for search bar called:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText on each search bar change. Each time a change received, the Model performs a partial match on data to construct a new array which is then displayed in the table.
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar when the Done (Return) key of keyboard tapped, the keyboard is dismissed.
#import <UIKit/UIKit.h> #import "Model.h" @interface ViewController : UITableViewController
// Convert a 6-character hex color to a UIColor object
- (UIColor *) getColor: (NSString *) hexColor
{
unsigned int red, green, blue;
NSRange range;
range.length = 2;
range.location = 0;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&red];
range.location = 2;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&green];
range.location = 4;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&blue];
return [UIColor colorWithRed:(float)(red/255.0f)
green:(float)(green/255.0f)
blue:(float)(blue/255.0f) alpha:1.0f];
}
|
Model
In the interest of completeness the Model class is provided.
There are several interesting points:
- Reading model data from a file named crayons.txt in the Other Sources. Here are a few entries:
Almond #EED9C4
Antique Brass #C88A65
Apricot #FDD5B1
Aquamarine #71D9E2and the code to read the file into colorArray:
NSString *pathname = [[NSBundle mainBundle] pathForResource:@"crayons" ofType:@"txt" inDirectory:@"/"];
NSString *wordstring = [NSString stringWithContentsOfFile: pathname];
colorArray = [[wordstring componentsSeparatedByString:@"\n"] retain];
- Performing partial matches on matchString with file entries to construct the array for table display; see -(void) buildSearchArrayFrom: (NSString *) matchString.
#import <Foundation/Foundation.h>
@interface Model : NSObject
{
NSMutableArray *colorArray;
NSMutableArray *searchArray;
}
@property (readonly, nonatomic, retain) NSMutableArray *searchArray;
- (void) buildSearchArrayFrom: (NSString *) matchString;
@end#import "Model.h"
@implementation Model
@synthesize searchArray;
// create an array by applying the search string
- (void) buildSearchArrayFrom: (NSString *) matchString
{
NSString *upString = [[matchString uppercaseString] autorelease];
if (searchArray) [searchArray release];
searchArray = [[NSMutableArray alloc] init];
for (NSString *word in colorArray)
{
if ([matchString length] == 0)
{
[searchArray addObject:word];
continue;
}
NSRange range = [[[[word componentsSeparatedByString:@" #"]
objectAtIndex:0] uppercaseString] rangeOfString:upString];
if (range.location != NSNotFound) [searchArray addObject:word];
}
}
-(id) init {
[super init];
// Retrieve the text and colors from file
NSString *pathname = [[NSBundle mainBundle] pathForResource:@"crayons"
ofType:@"txt"
inDirectory:@"/"];
NSString *wordstring = [NSString stringWithContentsOfFile:pathname];
colorArray = [[wordstring componentsSeparatedByString:@"\n"] retain];
[self buildSearchArrayFrom:@""];
return self;
}
// Clean up
-(void) dealloc {
[colorArray release];
[searchArray release];
[super dealloc];
}
@end
|