More Related Content
Similar to 201104 iphone navigation-based apps
Similar to 201104 iphone navigation-based apps (20)
More from Javier Gonzalez-Sanchez
More from Javier Gonzalez-Sanchez (20)
201104 iphone navigation-based apps
- 1. iOS Programming:
navigation-based applications
- 6. UITableViewDataSource
– tableView:cellForRowAtIndexPath:
- tableView:numberOfRowsInSection:
- tableView:titleForHeaderInSection:
- numberOfSectionsInTableView:
UITableViewDelegate
– tableView: didSelectRowAtIndexPath:
- tableView: heightForHeaderInSection
- tableView: viewForHeaderInSection
- 7. RootViewController.h
NSMutableArray
// RootViewController.h
#import <UIKit/UIKit.h>
@interface RootViewController : UITableViewController {
NSMutableArray* data;
}
@property (nonatomic, retain) NSMutableArray* data;
@end
- 8. RootViewController.m
NSMutableArray
// RootViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray* tmpArray =
[[NSMutableArray alloc]
initWithObjects:@”one”, @”two”, @”three”,nil];
self.data = tmpArray;
[tmpArray release];
}
- 9. RootViewController.m
TableViewDataSource
// RootViewController.m
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [self.data count];
}
- 10. RootViewController.m
TableViewDataSource
// RootViewController.m
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
cell.textLabel.text = [self.data objectAtIndex:indexPath.row];
return cell;
}
- 11. RootViewController.m
TableViewDataSource
// RootViewController.m
#import "RootViewController.h"
@implementation RootViewController
@synthesize data;
- (void)dealloc {
[data release];
[super viewDidLoad];
}
- 16. (1) plist - initialize
// RootViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle]
pathForResource:@"data" ofType:@"plist"];
NSMutableArray* tmpArray = [[NSMutableArray alloc]
initWithContentsOfFile:path];
self.data = tmpArray;
[tmpArray release];
}
- 19. file.h
// DetailViewController.h
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController {
IBOutlet UILabel *label;
}
@property (nonatomic, retain) UILabel *label;
@end
- 20. file.m
// DetailViewController.m
#import "DetailViewController.h"
@implementation DetailViewController
@synthesize label, text;
- (void)dealloc {
[super dealloc];
[label release];
[text release];
}
and connect the
outlet
- 22. RootViewController.m
// RootViewController.m
#import "RootViewController.h"
#import "DetailViewController.h"
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailViewController =
[[DetailViewController alloc]
initWithNibName:@"DetailViewController"
bundle:nil];
[self.navigationController
pushViewController:detailViewController
animated:YES];
[detailViewController release];
}
- 27. RootViewController.m
create cell from dictionary
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier]
autorelease];
}
// Configure the cell.
// cell.textLabel.text =
// [self.data objectAtIndex:indexPath.row];
cell.textLabel.text =
[[self.data objectAtIndex:indexPath.row] objectForKey:@"title"]
return cell;
}
- 28. RootViewController.m
// RootViewController.m
#import "RootViewController.h"
#import "DetailViewController.h"
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailViewController =
[[DetailViewController alloc]
initWithNibName:@"DetailViewController"
bundle:nil];
[self.navigationController
pushViewController:detailViewController
animated:YES];
detailViewController.row = [self.data objectAtIndex:indexPath.row];
[detailViewController release];
}
- 29. DetailView
NSDictionary *drink;
@property (nonatomic, retain) NSDictionary *drink;
@synthesize row
[drink release];
- (void) viewWillAppear: (BOOL)animated {
[super viewWillAppear:animated];
label.text = [row objectForKey:@"title"];
text.text = [drink objectForKey:@"desc"];
}
- 32. RootViewController.m
tableView cellForRowAtIndexPath:
cell = [[[UITableViewCell alloc]
initWithStyle: UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text = [
[self.data objectAtIndex:indexPath.row]
objectForKey:@"title"];
cell.detailTextLabel.text = [
[self.data objectAtIndex:indexPath.row]
objectForKey:@"desc"];
- 35. RootViewController.m
Table View - Grouped
// number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)
section {
! if (section == 0) return 2;
! if (section == 1) return 3;
! if (section == 2) return 1;
! else return 0;
}
// section
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:
(NSInteger)section {
! NSString *title = nil;
! switch (section) {
! ! case 0: title = @"Behavioral patterns"; break;
! ! case 1: title = @"Creational patterns"; break; XIB file:
! ! case 2: title = @"Structural patterns"; break;
! ! default: break; style = grouped
! }
! return title;
}
// number of section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- 37. ExampleAppDelegate.h
Adding a common iAds Banner
// MuralistasAppDelegate.h
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#define SharedAdBannerView
((MuralistasAppDelegate *)[[UIApplication sharedApplication] delegate]).banner
@interface MuralistasAppDelegate : NSObject <UIApplicationDelegate> {
ADBannerView *banner;!
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) ADBannerView *banner;
@end
- 38. ExampleAppDelegate.m
Adding a common iAds Banner
// didFinishLaunchingWithOptions
- (BOOL) application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Add the navigation controller's view to the window and display.
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
// Banner
banner = [[ADBannerView alloc] initWithFrame:CGRectZero];
// banner is pinned to the bottom
! self.banner.autoresizingMask =
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleTopMargin;
! // Supported landscape or portrait
[NSSet setWithObjects: ADBannerContentSizeIdentifier320x50,
ADBannerContentSizeIdentifier480x32, nil];
!
return YES;
}