Each section can have a title.
The title for the last section starts with a
'\n'
(newline character)
for extra vertical space.
Each section can also have a footer.
main.m
SectionAppDelegate
TableViewController
The
UITableViewController
acts as the
data
source
for the table view.
The
UITableViewController
must therefore have the following methods.
We have
already seen
the first three.
numberOfSectionsInTableView:
tableView:numberOfRowsInSection:
tableView:cellForRowAtIndexPath:
tableView:titleForHeaderInSection:
and
tableView:titleForFooterInSection:
.
Optional.
See
tableView:viewForHeaderInSection:
The
UITableViewController
also acts as the
delegate
for the table view.
The
UITableViewController
must therefore have the following method.
tableView:titleForFooterInSection:
UITableViewStyleGrouped
to
UITableViewStylePlain
in the
application:didFinishLaunchingWithOptions:
method of the application delegate.
Then change it back: the grouped style with the rounded edges looks better.
- (NSArray *) sectionIndexTitlesForTableView: (UITableView *) tv { return [NSArray arrayWithObjects: @"EST", @"CST", @"MST", @"PST", @"Misc.", nil]; } - (NSInteger) tableView: (UITableView *) tv sectionForSectionIndexTitle: (NSString *) title atIndex: (NSInteger) index { //one-to-one correspondence between sections and section index titles return index; }
tableView:titleForHeaderInSection:
tableView:titleForFooterInSection
sectionIndexTitlesForTableView:
Change
zones
(the array of five arrays) to an array of five dictionaries.
Each dictionary will contain four keys and four corresponding values.
The first key will be
@"header"
,
and the corresponding value will be the header for the section.
The second key will be
@"footer"
,
and the corresponding value will be the footer for the section.
The third key will be
@"index"
,
and the corresponding value will be
the index word along the right edge of the window.
The fourth key will be
@"names"
and the corresponding value will be the array of state names.
zones = [NSArray arrayWithObjects: [NSDictionary dictionaryWithObjectsAndKeys: @"EST: Eastern Standard Time", @"header", @"", @"footer", @"EST", @"index", [NSArray arrayWithObjects: @"Alabama", @"Connecticut", @"Delaware", //etc. nil ], @"names" nil ], [NSDictionary dictionaryWithObjectsAndKeys: @"CST: Central Standard Time", @"header", @"", @"footer", @"CST", @"index", [NSArray arrayWithObjects: @"Arkansas", @"Illinois", @"Iowa", //etc. nil ], @"names" nil ], //etc. nil ];Even better, put the above array of dictionaries in a separate object. The class of the object should be named
Model
.