TableViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface TableViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>//遵循tableview的委托协议和数据源协议 4 5 @property (copy, nonatomic) NSArray *tableData; 6 @end
TableViewController.m
1 #import "TableViewController.h" 2 3 @interface TableViewController () 4 5 @end 6 7 @implementation TableViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 // Do any additional setup after loading the view from its nib. 12 self.tableData = @[@"Bob", @"Fenndy", @"Neo", @"Lucky", @"Lily", @"Lucy", @"Jone", @"Kate", @"Sunny", @"Fenndy", @"Greeny", @"Brown", @"Jack", @"Andy"]; 13 self.tableData = [self.tableData sortedArrayUsingSelector:@selector(compare:)]; 14 } 15 16 - (void)didReceiveMemoryWarning { 17 [super didReceiveMemoryWarning]; 18 // Dispose of any resources that can be recreated. 19 } 20 21 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 22 return [self.tableData count]; 23 } 24 25 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 26 static NSString *Identifier = @"Cell"; 27 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier]; 28 if (cell == nil) { 29 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier]; 30 } 31 cell.textLabel.text = self.tableData[indexPath.row]; 32 return cell; 33 } 34 /* 35 #pragma mark - Navigation 36 37 // In a storyboard-based application, you will often want to do a little preparation before navigation 38 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 39 // Get the new view controller using [segue destinationViewController]. 40 // Pass the selected object to the new view controller. 41 } 42 */ 43 44 @end
AppDelegate.h
1 #import <UIKit/UIKit.h> 2 3 @interface AppDelegate : UIResponder <UIApplicationDelegate> 4 5 @property (strong, nonatomic) UIWindow *window; 6 7 8 @end
AppDelegate.m
1 #import "AppDelegate.h" 2 #import "TableViewController.h" 3 4 @interface AppDelegate () 5 6 @end 7 8 @implementation AppDelegate 9 10 11 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 12 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 13 // Override point for customization after application launch. 14 self.window.rootViewController = [[TableViewController alloc] initWithNibName:@"TableViewController" bundle:nil]; 15 self.window.backgroundColor = [UIColor whiteColor]; 16 [self.window makeKeyAndVisible]; 17 return YES; 18 } 19 20 - (void)applicationWillResignActive:(UIApplication *)application { 21 // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 22 // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 23 } 24 25 - (void)applicationDidEnterBackground:(UIApplication *)application { 26 // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 27 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 28 } 29 30 - (void)applicationWillEnterForeground:(UIApplication *)application { 31 // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 32 } 33 34 - (void)applicationDidBecomeActive:(UIApplication *)application { 35 // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 36 } 37 38 - (void)applicationWillTerminate:(UIApplication *)application { 39 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 40 } 41 42 @end
TableViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface TableViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>//遵循tableview的委托协议和数据源协议 4 5 @property (copy, nonatomic) NSArray *tableData; 6 @end
TableViewController.m
1 #import "TableViewController.h" 2 3 @interface TableViewController () 4 5 @end 6 7 @implementation TableViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 // Do any additional setup after loading the view from its nib. 12 self.tableData = @[@"Bob", @"Fenndy", @"Neo", @"Lucky", @"Lily", @"Lucy", @"Jone", @"Kate", @"Sunny", @"Fenndy", @"Greeny", @"Brown", @"Jack", @"Andy"]; 13 self.tableData = [self.tableData sortedArrayUsingSelector:@selector(compare:)]; 14 } 15 16 - (void)didReceiveMemoryWarning { 17 [super didReceiveMemoryWarning]; 18 // Dispose of any resources that can be recreated. 19 } 20 21 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 22 return [self.tableData count]; 23 } 24 25 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 26 static NSString *Identifier = @"Cell"; 27 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier]; 28 if (cell == nil) { 29 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier]; 30 } 31 cell.textLabel.text = self.tableData[indexPath.row]; 32 return cell; 33 } 34 /* 35 #pragma mark - Navigation 36 37 // In a storyboard-based application, you will often want to do a little preparation before navigation 38 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 39 // Get the new view controller using [segue destinationViewController]. 40 // Pass the selected object to the new view controller. 41 } 42 */ 43 44 @end
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 两个方法。
原文:http://www.cnblogs.com/nycoder/p/4369504.html