新一个空工程在appdelegate.m里面如下代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; RootViewController *rvc = [[RootViewController alloc] init]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rvc]; [rvc release]; self.window.rootViewController = nav; [nav release]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; }
新建一个继承与UIViewController的RootViewController
#import "RootViewController.h" #define kScreenSize [UIScreen mainScreen].bounds.size @interface RootViewController () <UITableViewDataSource,UITableViewDelegate> { NSMutableArray *_dataArr;//数据源数组(给TableView提供数据) UITableView *_tableView;//表格视图 } @end @implementation RootViewController - (void)dealloc { [_dataArr release]; [_tableView release]; [super dealloc]; } - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title=@"最简单的表格视图";//上面导航标题 [self dataInit]; [self creatTableView]; } //初始化数据 - (void)dataInit { //数据来源 //1.本地数据 (从沙盒中获取解析数据) 、写一些固定数据 //2.网络数据 (从服务器下载数据,下载完成之后,解析) //先创建一个空数组 _dataArr = [[NSMutableArray alloc] init]; //创建 100行的 固定数据放入数组中 for (NSInteger i = 0; i < 100; i++) { //100行字符串 NSString *str = [NSString stringWithFormat:@"第%ld行",i]; //把str放入到数组 [_dataArr addObject:str]; } } #pragma mark - 创建表格 - (void)creatTableView { //实例化一个表格视图对象 //UITableViewStylePlain普通样式 //UITableView是一个竖直滚动 水平固定的一个滚动视图, //UITableView的滚动范围 会根据内容自动计算 self.automaticallyAdjustsScrollViewInsets = NO; _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kScreenSize.width, kScreenSize.height-64) style:UITableViewStylePlain]; //设置数据源 (谁来给tableView 提供数据) _tableView.dataSource = self; //设置代理 _tableView.delegate = self; //UITableViewDataSource->里面的方法主要对tableView数据操作 //UITableViewDelegate--》里面的方法主要是tableView的UI操作 //把表格视图粘贴到self.view [self.view addSubview:_tableView]; } //cell 单元格 #pragma mark - UITableViewDataSource,UITableViewDelegate //设置 tableView 有 多少分区/组 //这个方法是可选 的 如果不写 默认是1个分区 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } //代理 来设置指定分区 有多少行 //行数 是和数据源数组 元素个数有关系 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _dataArr.count;//有多少元素就可以多少行cell } /* 重复/循环利用 1.飞机游戏的子弹 重复利用 生活中的例子 1.教室中的电脑 每期班 循环利用 2.饭店的餐具 */ // #if 1 //下面我们采用 cell 复用的形式 获取指定分区指定行的cell进行显示 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //复用标志 static NSString * cellId = @"Cell"; /* dequeueReusableCellWithIdentifier:内部采用了复用队列 每次调用dequeueReusableCellWithIdentifier:cellId 都会从复用队列中 查看 有没有 不在屏幕上显示的空闲的可用的cell //如果没有 返回 nil ,那么就创建新的cell //如果有可用的cell 返回cell地址 ,那么就复用 最多创建cell 个数 一屏 + 1个 只要cell 离开屏幕 就 会 空闲可复用 */ //从复用队列获取可用的 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId]; static NSInteger count = 0; if (cell == nil) { //表示没有 可复用的空闲的cell //创建新的cell cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId] autorelease]; count++; } NSLog(@"count:%ld",count); //获取指定的数据 // NSString *str = _dataArr[indexPath.row]; //放在cell上 //cell的内容标题 // cell.textLabel.text = str; cell.textLabel.text=_dataArr[indexPath.row]; return cell; } #else /* 下面的创建的cell方式 是有问题的 下面的写法会频繁的创建cell,这样的话直接降低了执行效率,浪费内存,屏幕 最多显示 一屏+1,实际上我们只需要创建一屏+1个cell 就可以了,这些cell 可以循环利用/重复利用,提高效率 ,如果一个cell离开屏幕了那么后面的cell 在显示的时候可以服用之前离开cell */ //cell 单元格 //创建 /获取 指定cell的地址并填充cell //要把cell 填充好数据返回 //创建指定分区 指定行的cell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellId = @"Cell"; //实例化一个cell UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId] autorelease]; //NSIndexPath就是一个索引路径 可以表示一个cell的确切的一个位置 //NSIndexPath表示第几分区 第几行 cell //indexPath.section 获取分区号 indexPath.row 获取行号 //从数据源获取指定行的数据 NSString *str = _dataArr[indexPath.row]; //填充cell 把数据放在cell 上 cell.textLabel.text = str; static NSInteger count = 0; count++; NSLog(@"count:%ld",count); return cell; } #endif - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
原文:http://my.oschina.net/u/2410306/blog/526127