文件类型
AppDelegate.m
#import "AppDelegate.h" #import "MainViewController.h" @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]; // MainViewController *mainVC = [[MainViewController alloc] init]; // UINavigationController *naviVC = [[UINavigationController alloc]initWithRootViewController:mainVC]; // self.window.rootViewController = naviVC; // [mainVC release]; //试图控制器和导航控制器的初始化 MainViewController *mainVC = [[MainViewController alloc] init]; UINavigationController *naviVC = [[UINavigationController alloc] initWithRootViewController:mainVC]; self.window.rootViewController = naviVC; [mainVC release]; [naviVC release]; [self.window makeKeyAndVisible]; [_window release]; return YES; } - (void)dealloc { [_window release]; [super dealloc]; } - (void)applicationWillResignActive:(UIApplication *)application { // 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. // 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. } - (void)applicationDidEnterBackground:(UIApplication *)application { // 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. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // 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. } - (void)applicationDidBecomeActive:(UIApplication *)application { // 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. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end
MainViewController.h
#import <UIKit/UIKit.h> @interface MainViewController : UIViewController @property (nonatomic , retain)NSMutableArray *array; //给tableView提供数组 @end
MainViewController.m
#import "MainViewController.h" #import "SecondViewController.h" //签订协议 @interface MainViewController ()<UITableViewDataSource , UITableViewDelegate> @end @implementation MainViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization self.array = [NSMutableArray arrayWithObjects:@"可口可乐", @"雪碧", @"奶茶", @"脉动", @"农夫山泉", @"绿茶", @"冰糖雪梨", nil]; } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.title = @"UITableView"; //UITableView的使用 // UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 310, 480) style:UITableViewStylePlain]; // [self.view addSubview:tableView]; // [tableView release]; UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain]; //实现tableView的协议方法 tableView.dataSource = self; tableView.delegate = self; //分割线的颜色 tableView.separatorColor = [UIColor blueColor]; //分割线的高度 tableView.rowHeight = 40; //给tableView添加一个顶部view UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)]; UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)]; view1.backgroundColor = [UIColor orangeColor]; view.backgroundColor = [UIColor blueColor]; tableView.tableHeaderView = view; //顶部view tableView.tableFooterView = view1; //底部view [view release]; [view1 release]; [self.view addSubview:tableView]; [tableView release]; //刷新数据 [tableView reloadData]; } //告诉tableView 每个分区(section) 显示多少行(row) - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.array count];//数组的行数 } //每一行(row)要显示的cell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //NSIndexPath有两个属性,一个是section,一个是row //一个indexPath就代表一个cell的位置 NSLog(@"%d",indexPath.row); // UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"啊哈哈哈"]; // //系统的cell,默认有三个控件 ,一个p_w_picpathView ,两label // cell.textLabel.text = @"雪碧供应商携款潜逃"; // cell.detailTextLabel.text = @"具体情况不明"; // return cell; //****************cell重用机制******************// //每当tableView要显示一个cell的时候,系统都会调用这个方法给tableView提供一个新的cell //每个tableView内部都有若干个cell的重用池,每当需要cell的时候,都去某一个重用池中取得一个cell //如果重用池中有cell,就直接使用.如果没有就创建一个新的cell,给cell一个重用标识,便于系统区分. //1.从重用池中尝试获取一个cell static NSString *sta = @"重用标识"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sta]; //2.判断是否取得一个cell if(cell == nil){ //如果取得的cell是nil,就创建一个新的cell cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:sta] autorelease]; NSLog(@"需要新的cell"); } NSString *name = [self.array objectAtIndex:indexPath.row]; //3.对cell重新赋值使用 cell.textLabel.text = name; cell.detailTextLabel.text = [NSString stringWithFormat:@"section:%d, row:%d",indexPath.section,indexPath.row]; return cell; } //返回多个section分区 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 5; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [NSString stringWithFormat:@"section:%d",section]; } //设置分区顶部的高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 30; } //自定义一个分区的顶部view - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *view = [[UIView alloc] init]; view.backgroundColor = [UIColor yellowColor]; return [view autorelease]; } //处理点击cell的事件 -----记住,谢谢 ... - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"%@", [self.array objectAtIndex:indexPath.row]); //初始化第二个页面,并且推出其二个页面 SecondViewController *secondVC = [[SecondViewController alloc] init]; [self.navigationController pushViewController:secondVC animated:YES]; [secondVC release]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #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
SecondViewController.m
#import "SecondViewController.h" @interface SecondViewController ()<UITableViewDataSource , UITableViewDelegate> @end @implementation SecondViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.title = @"第二页"; UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain]; tableView.dataSource = self; tableView.delegate = self; [self.view addSubview:tableView]; [tableView release]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 20; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *sta = @"aaa"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sta]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:sta] autorelease]; NSLog(@"需要新的cell"); } cell.textLabel.text = @"苹果公司发布会"; cell.detailTextLabel.text = [NSString stringWithFormat:@"section:%d, row:%d", indexPath.section, indexPath.row]; return cell; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #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
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。