文件类型

AppDelegate.m

#import"AppDelegate.h"#import"MainViewController.h"@implementationAppDelegate-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{self.window=[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];//Overridepointforcustomizationafterapplicationlaunch.self.window.backgroundColor=[UIColorwhiteColor];//MainViewController*mainVC=[[MainViewControlleralloc]init];//UINavigationController*naviVC=[[UINavigationControlleralloc]initWithRootViewController:mainVC];//self.window.rootViewController=naviVC;//[mainVCrelease];//试图控制器和导航控制器的初始化MainViewController*mainVC=[[MainViewControlleralloc]init];UINavigationController*naviVC=[[UINavigationControlleralloc]initWithRootViewController:mainVC];self.window.rootViewController=naviVC;[mainVCrelease];[naviVCrelease];[self.windowmakeKeyAndVisible];[_windowrelease];returnYES;}-(void)dealloc{[_windowrelease];[superdealloc];}-(void)applicationWillResignActive:(UIApplication*)application{//Sentwhentheapplicationisabouttomovefromactivetoinactivestate.Thiscanoccurforcertaintypesoftemporaryinterruptions(suchasanincomingphonecallorSMSmessage)orwhentheuserquitstheapplicationanditbeginsthetransitiontothebackgroundstate.//Usethismethodtopauseongoingtasks,disabletimers,andthrottledownOpenGLESframerates.Gamesshouldusethismethodtopausethegame.}-(void)applicationDidEnterBackground:(UIApplication*)application{//Usethismethodtoreleasesharedresources,saveuserdata,invalidatetimers,andstoreenoughapplicationstateinformationtorestoreyourapplicationtoitscurrentstateincaseitisterminatedlater.//Ifyourapplicationsupportsbackgroundexecution,thismethodiscalledinsteadofapplicationWillTerminate:whentheuserquits.}-(void)applicationWillEnterForeground:(UIApplication*)application{//Calledaspartofthetransitionfromthebackgroundtotheinactivestate;hereyoucanundomanyofthechangesmadeonenteringthebackground.}-(void)applicationDidBecomeActive:(UIApplication*)application{//Restartanytasksthatwerepaused(ornotyetstarted)whiletheapplicationwasinactive.Iftheapplicationwaspreviouslyinthebackground,optionallyrefreshtheuserinterface.}-(void)applicationWillTerminate:(UIApplication*)application{//Calledwhentheapplicationisabouttoterminate.Savedataifappropriate.SeealsoapplicationDidEnterBackground:.}@end


MainViewController.h

#import<UIKit/UIKit.h>@interfaceMainViewController:UIViewController@property(nonatomic,retain)NSMutableArray*array;//给tableView提供数组@end

MainViewController.m

#import"MainViewController.h"#import"SecondViewController.h"//签订协议@interfaceMainViewController()<UITableViewDataSource,UITableViewDelegate>@end@implementationMainViewController-(id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil{self=[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];if(self){//Custominitializationself.array=[NSMutableArrayarrayWithObjects:@"可口可乐",@"雪碧",@"奶茶",@"脉动",@"农夫山泉",@"绿茶",@"冰糖雪梨",nil];}returnself;}-(void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingtheview.self.title=@"UITableView";//UITableView的使用//UITableView*tableView=[[UITableViewalloc]initWithFrame:CGRectMake(0,0,310,480)style:UITableViewStylePlain];//[self.viewaddSubview:tableView];//[tableViewrelease];UITableView*tableView=[[UITableViewalloc]initWithFrame:CGRectMake(0,0,320,480)style:UITableViewStylePlain];//实现tableView的协议方法tableView.dataSource=self;tableView.delegate=self;//分割线的颜色tableView.separatorColor=[UIColorblueColor];//分割线的高度tableView.rowHeight=40;//给tableView添加一个顶部viewUIView*view=[[UIViewalloc]initWithFrame:CGRectMake(0,0,100,50)];UIView*view1=[[UIViewalloc]initWithFrame:CGRectMake(0,0,100,50)];view1.backgroundColor=[UIColororangeColor];view.backgroundColor=[UIColorblueColor];tableView.tableHeaderView=view;//顶部viewtableView.tableFooterView=view1;//底部view[viewrelease];[view1release];[self.viewaddSubview:tableView];[tableViewrelease];//刷新数据[tableViewreloadData];}//告诉tableView每个分区(section)显示多少行(row)-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{return[self.arraycount];//数组的行数}//每一行(row)要显示的cell-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{//NSIndexPath有两个属性,一个是section,一个是row//一个indexPath就代表一个cell的位置NSLog(@"%d",indexPath.row);//UITableViewCell*cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:@"啊哈哈哈"];////系统的cell,默认有三个控件,一个p_w_picpathView,两label//cell.textLabel.text=@"雪碧供应商携款潜逃";//cell.detailTextLabel.text=@"具体情况不明";//returncell;//****************cell重用机制******************////每当tableView要显示一个cell的时候,系统都会调用这个方法给tableView提供一个新的cell//每个tableView内部都有若干个cell的重用池,每当需要cell的时候,都去某一个重用池中取得一个cell//如果重用池中有cell,就直接使用.如果没有就创建一个新的cell,给cell一个重用标识,便于系统区分.//1.从重用池中尝试获取一个cellstaticNSString*sta=@"重用标识";UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:sta];//2.判断是否取得一个cellif(cell==nil){//如果取得的cell是nil,就创建一个新的cellcell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:sta]autorelease];NSLog(@"需要新的cell");}NSString*name=[self.arrayobjectAtIndex:indexPath.row];//3.对cell重新赋值使用cell.textLabel.text=name;cell.detailTextLabel.text=[NSStringstringWithFormat:@"section:%d,row:%d",indexPath.section,indexPath.row];returncell;}//返回多个section分区-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{return5;}-(NSString*)tableView:(UITableView*)tableViewtitleForHeaderInSection:(NSInteger)section{return[NSStringstringWithFormat:@"section:%d",section];}//设置分区顶部的高度-(CGFloat)tableView:(UITableView*)tableViewheightForHeaderInSection:(NSInteger)section{return30;}//自定义一个分区的顶部view-(UIView*)tableView:(UITableView*)tableViewviewForHeaderInSection:(NSInteger)section{UIView*view=[[UIViewalloc]init];view.backgroundColor=[UIColoryellowColor];return[viewautorelease];}//处理点击cell的事件-----记住,谢谢...-(void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath{NSLog(@"%@",[self.arrayobjectAtIndex:indexPath.row]);//初始化第二个页面,并且推出其二个页面SecondViewController*secondVC=[[SecondViewControlleralloc]init];[self.navigationControllerpushViewController:secondVCanimated:YES];[secondVCrelease];}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];//Disposeofanyresourcesthatcanberecreated.}/*#pragmamark-Navigation//Inastoryboard-basedapplication,youwilloftenwanttodoalittlepreparationbeforenavigation-(void)prepareForSegue:(UIStoryboardSegue*)seguesender:(id)sender{//Getthenewviewcontrollerusing[seguedestinationViewController].//Passtheselectedobjecttothenewviewcontroller.}*/@end

SecondViewController.m

#import"SecondViewController.h"@interfaceSecondViewController()<UITableViewDataSource,UITableViewDelegate>@end@implementationSecondViewController-(id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil{self=[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];if(self){//Custominitialization}returnself;}-(void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingtheview.self.title=@"第二页";UITableView*tableView=[[UITableViewalloc]initWithFrame:CGRectMake(0,0,320,480)style:UITableViewStylePlain];tableView.dataSource=self;tableView.delegate=self;[self.viewaddSubview:tableView];[tableViewrelease];}-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{return20;}-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{staticNSString*sta=@"aaa";UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:sta];if(cell==nil){cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:sta]autorelease];NSLog(@"需要新的cell");}cell.textLabel.text=@"苹果公司发布会";cell.detailTextLabel.text=[NSStringstringWithFormat:@"section:%d,row:%d",indexPath.section,indexPath.row];returncell;}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];//Disposeofanyresourcesthatcanberecreated.}/*#pragmamark-Navigation//Inastoryboard-basedapplication,youwilloftenwanttodoalittlepreparationbeforenavigation-(void)prepareForSegue:(UIStoryboardSegue*)seguesender:(id)sender{//Getthenewviewcontrollerusing[seguedestinationViewController].//Passtheselectedobjecttothenewviewcontroller.}*/@end