Permalink
Browse files

Created a root examples index view, in preparation for introducing ot…

…her examples.

Naturally, the examples controller uses a really simple table model that loads the
examples from a plist. Most of the boilerplate (notifying, keeping references to
listeners and the cell provider) have been moved into the first pass at an abstract
table model in the LRTableModel package.
  • Loading branch information...
lukeredpath committed Aug 10, 2010
1 parent 53a0572 commit 0ef65e077d588ef9d9194a076954079addf960c0
@@ -0,0 +1,23 @@
+//
+// ExamplesIndexViewController.h
+// TableViewModel
+//
+// Created by Luke Redpath on 10/08/2010.
+// Copyright 2010 LJR Software Limited. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "LRAbstractTableModel.h"
+
+@interface ExamplesTableModel : LRAbstractTableModel
+{
+ NSArray *examples;
+}
+- (void)loadExamplesFromPlistNamed:(NSString *)plistName inBundle:(NSBundle *)bundle;
+@end
+
+@interface ExamplesIndexViewController : UITableViewController <LRTableModelEventListener, LRTableModelCellProvider> {
+ ExamplesTableModel *examplesTableModel;
+}
+@property (nonatomic, readonly) ExamplesTableModel *examplesTableModel;
+@end
@@ -0,0 +1,115 @@
+//
+// ExamplesIndexViewController.m
+// TableViewModel
+//
+// Created by Luke Redpath on 10/08/2010.
+// Copyright 2010 LJR Software Limited. All rights reserved.
+//
+
+#import "ExamplesIndexViewController.h"
+#import "LRTableModelEvent.h"
+
+@implementation ExamplesTableModel
+
+- (id)initWithCellProvider:(id <LRTableModelCellProvider>)theCellProvider
+{
+ if (self = [super initWithCellProvider:theCellProvider]) {
+ examples = [[NSMutableArray alloc] init];
+ }
+ return self;
+}
+
+- (void)dealloc
+{
+ [examples release];
+ [super dealloc];
+}
+
+- (void)loadExamplesFromPlistNamed:(NSString *)plistName inBundle:(NSBundle *)bundle
+{
+ examples = [[NSArray arrayWithContentsOfFile:[bundle pathForResource:plistName ofType:@"plist"]] retain];
+ [self notifyListeners:[LRTableModelEvent refreshed]];
+}
+
+- (NSInteger)numberOfSections;
+{
+ return 1;
+}
+
+- (NSInteger)numberOfRows;
+{
+ return [examples count];
+}
+
+- (id)objectAtIndexPath:(NSIndexPath *)indexPath;
+{
+ return [examples objectAtIndex:indexPath.row];
+}
+
+@end
+
+@implementation ExamplesIndexViewController
+
+- (void)dealloc
+{
+ [examplesTableModel release];
+ [super dealloc];
+}
+
+- (ExamplesTableModel *)examplesTableModel
+{
+ if (examplesTableModel == nil) {
+ examplesTableModel = [[ExamplesTableModel alloc] initWithCellProvider:self];
+ [examplesTableModel addTableModelListener:self];
+ }
+ return examplesTableModel;
+}
+
+- (void)viewDidLoad
+{
+ [super viewDidLoad];
+
+ self.tableView.rowHeight = 65;
+ self.tableView.dataSource = self.examplesTableModel;
+
+ [self.examplesTableModel loadExamplesFromPlistNamed:@"Examples" inBundle:[NSBundle mainBundle]];
+}
+
+#pragma mark -
+#pragma mark LRTableModel methods
+
+- (void)tableModelChanged:(LRTableModelEvent *)changeEvent
+{
+ [self.tableView reloadData];
+}
+
+- (NSString *)cellReuseIdentifierForIndexPath:(NSIndexPath *)indexPath
+{
+ return @"ExampleCell";
+}
+
+- (UITableViewCell *)cellForObjectAtIndexPath:(NSIndexPath *)indexPath reuseIdentifier:(NSString *)reuseIdentifier
+{
+ return [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier] autorelease];
+}
+
+- (void)configureCell:(UITableViewCell *)cell forObject:(id)object atIndexPath:(NSIndexPath *)indexPath
+{
+ cell.textLabel.text = [object valueForKey:@"name"];
+ cell.detailTextLabel.text = [object valueForKey:@"description"];
+ cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
+}
+
+#pragma mark -
+#pragma mark UITableViewDelegate
+
+- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
+{
+ NSDictionary *exampleData = [self.examplesTableModel objectAtIndexPath:indexPath];
+
+ UIViewController *exampleViewController = [[NSClassFromString([exampleData valueForKey:@"controller"]) alloc] init];
+ [self.navigationController pushViewController:exampleViewController animated:YES];
+ [exampleViewController release];
+}
+
+@end
@@ -0,0 +1,17 @@
+//
+// LRAbstractTableModel.h
+// TableViewModel
+//
+// Created by Luke Redpath on 10/08/2010.
+// Copyright 2010 LJR Software Limited. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "LRTableModel.h"
+
+@interface LRAbstractTableModel : NSObject <LRTableModel> {
+ NSMutableArray *eventListeners;
+ id<LRTableModelCellProvider> cellProvider;
+}
+- (void)notifyListeners:(LRTableModelEvent *)event;
+@end
@@ -0,0 +1,88 @@
+//
+// LRAbstractTableModel.m
+// TableViewModel
+//
+// Created by Luke Redpath on 10/08/2010.
+// Copyright 2010 LJR Software Limited. All rights reserved.
+//
+
+#import "LRAbstractTableModel.h"
+
+
+@implementation LRAbstractTableModel
+
+- (id)initWithCellProvider:(id<LRTableModelCellProvider>)theCellProvider;
+{
+ if (self = [super init]) {
+ eventListeners = [[NSMutableArray alloc] init];
+ cellProvider = [theCellProvider retain];
+ }
+ return self;
+}
+
+- (void)dealloc
+{
+ [cellProvider release];
+ [eventListeners release];
+ [super dealloc];
+}
+
+- (void)notifyListeners:(LRTableModelEvent *)event;
+{
+ for (id<LRTableModelEventListener> listener in eventListeners) {
+ [listener tableModelChanged:event];
+ }
+}
+
+- (void)addTableModelListener:(id<LRTableModelEventListener>)listener;
+{
+ [eventListeners addObject:listener];
+}
+
+- (void)removeTableModelListener:(id<LRTableModelEventListener>)listener;
+{
+ [eventListeners removeObject:listener];
+}
+
+- (NSInteger)numberOfSections; // abstract
+{
+ return 1;
+}
+
+- (NSInteger)numberOfRows; // abstract
+{
+ return 0;
+}
+
+- (id)objectAtIndexPath:(NSIndexPath *)indexPath; // abstract
+{
+ return nil;
+}
+
+#pragma mark -
+#pragma mark UITableViewDataSource methods
+
+- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
+{
+ NSString *reuseIdentifier = [cellProvider cellReuseIdentifierForIndexPath:indexPath];
+
+ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
+ if (cell == nil) {
+ cell = [cellProvider cellForObjectAtIndexPath:indexPath reuseIdentifier:reuseIdentifier];
+ }
+ [cellProvider configureCell:cell forObject:[self objectAtIndexPath:indexPath] atIndexPath:indexPath];
+
+ return cell;
+}
+
+- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
+{
+ return [self numberOfSections];
+}
+
+- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
+{
+ return [self numberOfRows];
+}
+
+@end
@@ -8,11 +8,12 @@
#import <UIKit/UIKit.h>
-@interface TableViewModelAppDelegate : NSObject <UIApplicationDelegate> {
- UIWindow *window;
+@interface TableViewModelAppDelegate : NSObject <UIApplicationDelegate>
+{
+ UIWindow *window;
+ UINavigationController *navigationController;
}
-
@property (nonatomic, retain) IBOutlet UIWindow *window;
-
+@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@end
@@ -12,17 +12,13 @@
@implementation TableViewModelAppDelegate
@synthesize window;
-
+@synthesize navigationController;
#pragma mark -
#pragma mark Application lifecycle
-- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
-
- SimpleTableViewController *tableViewController = [[SimpleTableViewController alloc] initWithStyle:UITableViewStylePlain];
- self.window.rootViewController = [[[UINavigationController alloc] initWithRootViewController:tableViewController] autorelease];
- [tableViewController release];
-
+- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
+{
[window makeKeyAndVisible];
return YES;
@@ -77,9 +73,11 @@ Free up as much memory as possible by purging cached data objects that can be re
}
-- (void)dealloc {
- [window release];
- [super dealloc];
+- (void)dealloc
+{
+ [navigationController release];
+ [window release];
+ [super dealloc];
}
View
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<array>
+ <dict>
+ <key>name</key>
+ <string>SimpleTableModel</string>
+ <key>description</key>
+ <string>A basic, array-backed table model</string>
+ <key>controller</key>
+ <string>SimpleTableViewController</string>
+ </dict>
+</array>
+</plist>
Oops, something went wrong.

0 comments on commit 0ef65e0

Please sign in to comment.