Created
March 3, 2012 16:48
-
-
Save koba04/1966940 to your computer and use it in GitHub Desktop.
UITableView's Sample
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 TableViewSampleViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{ | |
UITableView *sampleTable; | |
NSArray *tableData; | |
} | |
@property (nonatomic, retain) IBOutlet UITableView *sampleTable; | |
@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 "TableViewSampleViewController.h" | |
@implementation TableViewSampleViewController | |
@synthesize sampleTable; | |
- (void)dealloc | |
{ | |
[sampleTable release]; | |
[tableData release]; | |
[super dealloc]; | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
// Releases the view if it doesn't have a superview. | |
[super didReceiveMemoryWarning]; | |
// Release any cached data, images, etc that aren't in use. | |
} | |
#pragma mark - View lifecycle | |
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
tableData = [[NSArray alloc]initWithObjects:@"新宿", @"代々木", @"原宿", @"渋谷", nil]; | |
} | |
- (void)viewDidUnload | |
{ | |
[self setSampleTable:nil]; | |
[super viewDidUnload]; | |
// Release any retained subviews of the main view. | |
// e.g. self.myOutlet = nil; | |
} | |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation | |
{ | |
// Return YES for supported orientations | |
return (interfaceOrientation == UIInterfaceOrientationPortrait); | |
} | |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
static NSString *cellID = @"SampleView"; | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; | |
if (cell == nil) { | |
cell = [[[UITableViewCell alloc]initWithFrame:CGRectZero reuseIdentifier:cellID]autorelease]; | |
} | |
cell.textLabel.text = [tableData objectAtIndex:indexPath.row]; | |
return cell; | |
} | |
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { | |
return [tableData count]; | |
} | |
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { | |
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"山手線!" | |
message:[tableData objectAtIndex:indexPath.row] | |
delegate:self | |
cancelButtonTitle:@"OK" | |
otherButtonTitles: nil | |
]; | |
[alert show]; | |
[alert release]; | |
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment