UITableViewï¼
ä»åã¯iPhoneã®ä¸ã§ããã使ãããã§ãããUITableViewã使ç¨ãã¦ã¿ããã¨æãã¾ãã
TableViewã使ãéã«ã¯UITableViewã¨UITableViewControllerã®äºã¤ã®é¸æè¢ãããã®ã§ãã
ä»åã¯UITableViewControllerã使ç¨ãã¾ãã
UITableViewControllerã¯UITableViewãããèªåçã«ãããããã£ã¦ããã¾ãã
ä¾ãã°ãããªãã¨ã
- ãã¼ãã«ã表示ãããé(viewWillAppear:)ã«ããã¼ã¿ã®ãªãã¼ããé¸æè¡ã®è§£é¤ã
- ãã¼ãã«ã表示ãããå¾(viewDidAppear:)ã«ãã¹ã¯ãã¼ã«ãã¼ã®ç¹æ» ã
- ããã²ã¼ã·ã§ã³ãã¼ã®ç·¨é/å®äºãã¿ã³ãæ¼ããã¨ãã«ç·¨é/é常ã¢ã¼ãã«ç§»è¡ã
ãããªã¢ããªãåºæ¥ä¸ããã¯ãã§ããã
ãã¦ä»åãTableDemoã¨ããååã§ãWindows-Based Applicationãã§ä½æãã¾ãã
ããã§ã§ããããã¸ã§ã¯ãå ã®ãã©ã«ããClassesããé¸æã
ããã¡ã¤ã«ãâãæ°è¦ãã¡ã¤ã«ãâãUIviewController subClassããé¸æãTableDemoViewControllerã¨ååãä»ãã¦ä¿åãã¾ãã
ãã¤ãéããInterface Builderã¯åã£ã¦ãã ãããã
ã§ã¯ããã¤ãã©ãããViewControllerããå§ãã¾ãã
ã§ããæ¬ã®éãæã¡è¾¼ãã æãã§ã½ã¼ã¹ãèªãã§ãå°ãåãããªãã¨ããããã¾ãããã
iPhone SDK Application Development: Building Applications for the AppStore
- ä½è : Jonathan Zdziarski
- åºç社/ã¡ã¼ã«ã¼: O'Reilly Media
- çºå£²æ¥: 2009/02/06
- ã¡ãã£ã¢: ãã¼ãã¼ããã¯
- è³¼å ¥: 2人 ã¯ãªãã¯: 110å
- ãã®ååãå«ãããã° (14件) ãè¦ã
TableDemoViewController.h
#import <UIKit/UIKit.h> // TableViewã使ãã®ã§UIViewContorollerã使ãã®ã§ã¯ãªããUITableViewControllerã使ã @interface TableDemoViewController : UITableViewController { // NSMUtableArrayã¯ã¤ã³ã¹ã¿ã³ã¹ä½æå¾ãå¯å¤ãªãµã¤ãºã®é å // å¯å¤ãããªãã®ã¯NSArray NSMutableArray *fileList; } -(void) startEditing; -(void) stopEditing; -(void) reload; @end
TableDemoViewController.m
#import "TableDemoViewController.h" @implementation TableDemoViewController -(id) init{ self = [super init]; if(self != nil){ [self reload]; // navigation Barã®å³å´ã«[Edit]ãã¿ã³ã表示ããã self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(startEditing)] autorelease]; // navigation Barã®å·¦å´ã«[reload]ãã¿ã³ã表示ããã self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"reload" style:UIBarButtonItemStylePlain target:self action:@selector(reload)] autorelease]; } return self; } -(void)startEditing{ // ç·¨éã¢ã¼ãã«ãã [self.tableView setEditing:YES animated:YES]; // navigation Barã®å³å´ã«[Done]ãã¿ã³ã表示ããã self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(stopEditing)] autorelease]; } -(void)stopEditing{ // ç·¨éã¢ã¼ããããã [self.tableView setEditing:NO animated:YES]; // navigation Barã®å³å´ã«[Edit]ãã¿ã³ã表示ããã self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(startEditing)] autorelease]; } -(void) reload{ // NSDirectoryEnumeratorã¯ãã£ã¬ã¯ããªå ã®ãã¡ã¤ã«åãåãåºããããã«ãã NSDirectoryEnumerator *dirEnum; NSString *file; fileList = [[NSMutableArray alloc] init]; // homeãã£ã¬ã¯ããªå ã®ãã¡ã¤ã«åãæ ¼ç´ãã dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:NSHomeDirectory()]; // ãã¡ã¤ã«åã次ã ã«åãåºã while((file = [dirEnum nextObject])){ [fileList addObject:file]; } [self.tableView reloadData]; } // numberOfSectionsInTableViewã¯ãã¼ãã«å ã®ã»ã¯ã·ã§ã³æ°ãè¿ããä»åã¯1å -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } // numberOfRowsInSectionã¯ã»ã¯ã·ã§ã³å ã®è¡æ°ãè¿ãã -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [fileList count]; } // cellForRowAtIndexPathã¯ã»ã«ã®ä¸èº«ã表示ãã(ãããå°ãç解ããããµãããã) -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // fileListã®1è¡ç®ã表示ããï¼ NSString *CellIdentifier = [fileList objectAtIndex:[indexPath indexAtPosition:1]]; // ã»ã«ãä½æãã UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // 以åã¤ãã£ãcellã®ãã¼ã¿ãããã®ã if(cell == nil){ // ç¡ããã°ä½æãã cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; cell.text = CellIdentifier; UIFont *font = [UIFont fontWithName:@"Courier" size:12.0]; cell.font = font; } return cell; } // commitEditingStyleã¯Editå¼ã°ããæã«åé¤ã¢ã¼ãã«ãªãããã«å¼ã°ããã¡ã½ããã£ã½ãï¼ -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle) editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if(editingStyle == UITableViewCellEditingStyleDelete){ UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; for(int i=0; i < [fileList count]; i++){ if([cell.text isEqualToString:[fileList objectAtIndex:i]]){ [fileList removeObjectAtIndex:i]; } } NSMutableArray *array = [[NSMutableArray alloc] init]; [array addObject:indexPath]; [self.tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationTop]; } } // didSelectRowAtIndexPathã¯ã»ã«ãã¿ããããããã³ã¼ã«ããã -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; // ã»ã«ãã¿ããããããAlertã®ç»é¢ãåºã UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"File Selected" message:[NSString stringWithFormat:@"You selected the File '%@'", cell.text] delegate:nil cancelButtonTitle:nil otherButtonTitles: @"OK", nil]; [ alert show]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return YES; } - (void)loadView { [super loadView]; } - (void)dealloc { [fileList release]; [super dealloc]; } @end
ã³ã¡ã³ããæ¸ãã¦ããã¾ããã
cellForRowAtIndexPathã¨commitEditingStyleãå°ããããµãããã
ããå°ã調ã¹ã¦ã¿ããã¨æãã¾ããããã
Delegateã®ã½ã¼ã¹ã¯ãä»ã¾ã§ã¨åæ§ViewControllerãã¤ã³ã¹ã¿ã³ã¹åãã¦windowã«subviewã¨ãã¦è²¼ä»ãã¦ãã ãã§ãã
TableDemoAppDelegate.h
#import <UIKit/UIKit.h> @class TableDemoViewController; @interface TableDemoAppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window; TableDemoViewController *viewController; UINavigationController *navigationController; } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet TableDemoViewController *viewController; @end
TableDemoAppDelegate.m
#import "TableDemoAppDelegate.h" #import "TableDemoViewController.h" @implementation TableDemoAppDelegate @synthesize window; @synthesize viewController; - (void)applicationDidFinishLaunching:(UIApplication *)application { window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; viewController = [[TableDemoViewController alloc] init]; navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; [window addSubview:[navigationController view]]; // Override point for customization after application launch [window makeKeyAndVisible]; } - (void)dealloc { [window release]; [super dealloc]; } @end
ãã¼ãã«ã§ä¸æ°ã«æºåãã¦ãããªãããªããªãã¡ã½ãããå¢ãã¾ããããã
ãã¡ãã£ã¨ç解ã¯æ·±ããããªã