Instantly share code, notes, and snippets.
Created
September 15, 2010 10:37
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save abitgone/580544 to your computer and use it in GitHub Desktop.
Very Basic UITableViewController Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface MainMenuController : UITableViewController { | |
NSMutableArray *menuSections; | |
} | |
@property (nonatomic, retain) NSMutableArray *menuSections; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "MainMenuController.h" | |
@implementation MainMenuController | |
@synthesize menuSections; | |
#pragma mark - | |
#pragma mark View Lifecycle | |
- (void)viewWillAppear:(BOOL)animated { | |
// Don't show the toolbar on the main menu - there's nothing to go in there | |
[self.navigationController setToolbarHidden:YES animated:YES]; | |
// Set the title to "Home" for the back button on submenu pages, and replace with a nice Listers Logo | |
self.navigationItem.title = @"Home"; | |
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"menu_navbar.png"]]; | |
menuSections = [[NSMutableArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"MainMenu" ofType:@"plist"]]; | |
// As you were... | |
[super viewWillAppear:animated]; | |
} | |
#pragma mark - | |
#pragma mark Table view data source | |
// Customize the number of sections in the table view. | |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { | |
return [self.menuSections count]; | |
} | |
// Customize the number of rows in the table view. | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { | |
NSDictionary *sectionContents = [self.menuSections objectAtIndex:section]; | |
NSArray *sectionItems = [sectionContents objectForKey:@"Sections"]; | |
return [sectionItems count]; | |
[sectionItems release]; | |
[sectionContents release]; | |
} | |
// Customise the appearance of the table's sections | |
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { | |
NSDictionary *sectionContents = [self.menuSections objectAtIndex:section]; | |
NSString *sectionTitle = [sectionContents objectForKey:@"Title"]; | |
return sectionTitle; | |
[sectionTitle release]; | |
[sectionContents release]; | |
} | |
- (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { | |
NSDictionary *sectionContents = [self.menuSections objectAtIndex:section]; | |
NSString *sectionFooter = [sectionContents objectForKey:@"Footer"]; | |
return sectionFooter; | |
[sectionFooter release]; | |
[sectionContents release]; | |
} | |
// Customize the appearance of table view cells. | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
static NSString *CellIdentifier = @"MainMenuCell"; | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; | |
if (cell == nil) { | |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; | |
} | |
NSDictionary *currentRow = [[[self.menuSections objectAtIndex:indexPath.section] | |
objectForKey:@"Sections"] | |
objectAtIndex:indexPath.row]; | |
UIImage *rowImage = [UIImage imageNamed:[currentRow objectForKey:@"RowImage"]]; | |
UIImage *rowImageHighlighted = [UIImage imageNamed:[currentRow objectForKey:@"RowImageHighlighted"]]; | |
// Configure the cell. | |
cell.textLabel.text = [currentRow objectForKey:@"Title"]; | |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; | |
if (rowImage != nil) { | |
cell.imageView.image = rowImage; | |
if (rowImageHighlighted != nil) { | |
cell.imageView.highlightedImage = rowImageHighlighted; | |
} | |
} | |
// And return it | |
return cell; | |
// Cleanup | |
[rowImage release]; | |
[rowImageHighlighted release]; | |
[currentRow release]; | |
} | |
#pragma mark - | |
#pragma mark Table view delegate | |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { | |
NSDictionary *currentRow = [[[self.menuSections objectAtIndex:indexPath.section] | |
objectForKey:@"Sections"] | |
objectAtIndex:indexPath.row]; | |
NSString *sectionType = [currentRow objectForKey:@"Type"]; | |
// At this point, we'll perform some logic to decide what viewcontroller to load next | |
// Deselect the row | |
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES]; | |
} | |
#pragma mark - | |
#pragma mark Memory management | |
- (void)didReceiveMemoryWarning { | |
// Releases the view if it doesn't have a superview. | |
[super didReceiveMemoryWarning]; | |
} | |
- (void)viewDidUnload { | |
self.menuSections = nil; | |
[super viewDidUnload]; | |
} | |
- (void)dealloc { | |
[self.menuSections release]; | |
[super dealloc]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment