与UIViewController有区别,就是UITableViewController自身的带的是tableView。
AppDelegate.m
#import "AppDelegate.h"
#import "RootTableViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
RootTableViewController *rootTVC = [[RootTableViewController alloc]initWithStyle:(UITableViewStyleGrouped)];
UINavigationController *ncRoot = [[UINavigationController alloc]initWithRootViewController:rootTVC];
self.window.rootViewController = ncRoot;
[self.window makeKeyAndVisible];
[ncRoot release];
ncRoot = nil;
[rootTVC release];
rootTVC = nil;
return YES;
}
解释:
1、与原来的UIViewController一样,创建出来的RootTableViewController的对象rootTVC,style可以用plain和grouped两种。
2、UINavigationController创建出来是用rootTVC作为参数创建的。
RootTableViewController.m
#import "AppDelegate.h"
#import "RootTableViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
RootTableViewController *rootTVC = [[RootTableViewController alloc]initWithStyle:(UITableViewStyleGrouped)];
UINavigationController *ncRoot = [[UINavigationController alloc]initWithRootViewController:rootTVC];
self.window.rootViewController = ncRoot;
[self.window makeKeyAndVisible];
[ncRoot release];
ncRoot = nil;
[rootTVC release];
rootTVC = nil;
return YES;
}
解释:
1、与原来的UIViewController一样,创建出来的RootTableViewController的对象rootTVC,style可以用plain和grouped两种。
2、UINavigationController创建出来是用rootTVC作为参数创建的。
RootTableViewController.m
#import "RootTableViewController.h"
#define kCELL_ID @"cell_1"
@interface RootTableViewController ()
@end
@implementation RootTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 注册,注册要用的哪个cell
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kCELL_ID];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCELL_ID forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"section:%ld,row:%ld",indexPath.section,indexPath.row];
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
解释:
1、UITableViewController已经帮我们封装了我们实现代理那些方法,就是上面那些注释起来的方法。
2、[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kCELL_ID],是对我们将要用到的cell进行注册。第一个参数,是UITableViewCell类,第二个参数,是我们设的宏定义,用来作为cell的重用字符。