Skip to content

Instantly share code, notes, and snippets.

@koba04
Created March 3, 2012 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koba04/1966940 to your computer and use it in GitHub Desktop.
Save koba04/1966940 to your computer and use it in GitHub Desktop.
UITableView's Sample
#import <UIKit/UIKit.h>
@interface TableViewSampleViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{
UITableView *sampleTable;
NSArray *tableData;
}
@property (nonatomic, retain) IBOutlet UITableView *sampleTable;
@end
#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