iPhone开发【七】常用控件之表TableView
来源:互联网 发布:淘宝店铺职位 编辑:程序博客网 时间:2024/06/15 09:16
转载请注明出处,原文网址:http://blog.csdn.net/m_changgong/article/details/8115137 作者:张燕广
实现的功能:演示表TableView的使用方法
关键词:TableView
1、创建一个Single View Application工程,命名为:TableViewDemo,如下图
2、修改ViewController.xib,添加一个Table View控件,连接操作,如下
3、视图控制器ViewController,需要实现协议UITableViewDataSource、UITableViewDelegate中的必须实现的方法,
在工程目录依次展开Frameworks->UIKit.framework->Headers,然后打开UITableView.h,搜索找到协议UITableViewDataSource、UITableViewDelegate的定义,如下
4、修改ViewController.h,如下
//// ViewController.h// TableViewDemo//// Created by Zhang Yanguang on 12-10-25.// Copyright (c) 2012年 MyCompanyName. All rights reserved.//#import <UIKit/UIKit.h>@interface ViewController : UIViewController@property(nonatomic,retain)NSMutableArray *apps;@end
5、修改ViewController.m,如下
//// ViewController.m// TableViewDemo//// Created by Zhang Yanguang on 12-10-25.// Copyright (c) 2012年 MyCompanyName. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController@synthesize apps;- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. //加载数据 [self loadData];}//模拟加载数据-(void)loadData{ apps = [[NSMutableArray alloc]init]; for(int i=0;i<8;i++){ [apps insertObject:[NSString stringWithFormat:@"App-%d",(i+1)] atIndex:i]; }}- (void)viewDidUnload{ [super viewDidUnload]; apps = nil; // Release any retained subviews of the main view.}-(void)dealloc{ [super dealloc]; [apps release];}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);}#pragma mark table view datasource methods //可选实现的方法,默认为1- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;{ return 1;}//必须实现的方法- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [apps count];}//必须实现的方法- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // NSString *myTableViewCellIdentifier = @"myTableViewCellIdentifier"; UITableViewCell *cell = [[UITableView alloc]dequeueReusableCellWithIdentifier:myTableViewCellIdentifier];//备注1 if(cell==nil){ cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myTableViewCellIdentifier]; //备注2 } NSString *imageName = [NSString stringWithFormat:@"%d",[indexPath row]+1]; //cell.image = [UIImage imageNamed:imageName]; //这种用法已经废弃 cell.imageView.image = [UIImage imageNamed:imageName]; cell.textLabel.text = [apps objectAtIndex:[indexPath row]]; cell.textLabel.textAlignment = UITextAlignmentLeft; cell.textLabel.font = [cell.textLabel.font fontWithSize:30]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell;}//可选实现的方法- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return @"Header";}//可选实现的方法- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ return @"Footer";}#pragma mark table view data delegate methods- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 50;}@end
备注1、备注2,代码解释:1)表中的每一行都拥有一个子视图,即一个UITableViewCell类的实例
2)对于一个拥有大量数据的表,如果UITableView为表中的每一行都分配一个UITableViewCell的实例,而不管该行是否正在显示,这无疑将带来大量内容开销。当然,UITableView并不是这样设计的。
3)只有当前显示的行才被分配UITableViewCell的实例,因滚动操作已离开屏幕的表视图单元(UITableViewCell的实例)将被放置在一个可以被重用的单元序列中。
如果系统运行比较缓慢,表视图就从该序列中删除这些单元,以释放存储空间;
只要有可用的存储空间,表视图就会重新获取这些单元,以便以后再次使用。
4)//当一个表单视图滚出屏幕时,另一个表单视图就会从另一边滚动到屏幕上,如果滚动到屏幕上的新行重新使用从屏幕上滚动下来的那些单元格中的某一个单元格,系统就会避免与不断创建和释放那些视图相关的开销。
5)为了使用此机制,需要一个标识符(identifier)来标示可重用的单元格
6)表第一次初始化时肯定没有可重用的表单元,所以需要初始分配:
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myTableViewCellIdentifier];
reuseIdentifier参数即标示该表单元可重用的标示符
6、编译、运行,效果如下
点击下载本文源代码
- iPhone开发【七】常用控件之表TableView
- iPhone开发【七】常用控件之表TableView
- iphone开发之TableView控件实例
- iPhone开发之TableView
- iPhone 开发常用控件
- iphone开发之常用控件的使用详解
- iPhone开发【四】常用控件之ActionSheet与AlertView
- iPhone开发【六】常用控件之Picker View
- iPhone开发【四】常用控件之ActionSheet与AlertView
- iPhone开发【六】常用控件之Picker View
- iphone常用控件之UIScrollView
- iphone常用控件之UIScrollView
- iphone常用控件之UIScrollView
- iphone练习之TableView
- iphone练习之TableView
- iphone练习之TableView
- IPHONE应用开发 - 常用控件介绍
- IPHONE应用开发 - 常用控件介绍
- IOS--颜色转换 数组排序 事件转换
- 面试中的几个题
- RSync实现文件备份同步
- php-cgi中一个常见的错误
- iBatis
- iPhone开发【七】常用控件之表TableView
- 傻孩子。痛了?放手吧:伤感日志
- 服务器和应用系统迁移方案
- 修改Tiny6410Android系统的开机默认设置
- 辗转相除法求两数的最大公约数(递归实现)
- 关于dyld: Library not loaded
- qt中文乱码(转)
- C在win32的console下响应CTRL+C
- 那些日夜想念你的泪水 是咸的我的心还是会痛