iOS 表视图(UITableVIew)的使用方法(1)表视图的示例
2014-04-16 12:47
661 查看
表视图继承自UIScrollView,所以有着大多UIScrollView的操作特性,诸如手指控制内容的滚动,内容视图到顶端或者低端时的自动反弹等。配合UINavigationController的导航特性,表视图可以将大量有一定规则顺序的数据,完整的呈现到客户端上。
一般,开发者可以将UITableView的datasource和delegate对象设置成同一个控制器对象,delegate的回调函数并非强制实现,如果控制器没有特别实现代理回调函数的话,UITableView将用默认的值代替,比如默认无页眉页脚,默认点击UITableViewCell时不做任何处理等。
但是datasource却不同,想要正常显示一个UITableView,作为datasource的对象需要强制地实现两个回调方法。
表视图的使用最适合的场景时数据列表,我们以一份恒大足球队的球员名单来做示例,数据存储用苹果程序最喜欢的plist文件格式记录。如下图:
每个球员一共有三个信息:球衣号码,球员名字和角色
在具体工作开展之前,有必要为这个结构声明一个数据模型,代码如下:
HBPlayerInfo.h文件
随后在UITableViewControll的子类取名SimpleTabelViewController,对于头文件,声明如下:
头文件声明了2个初始化的方法,对于数据源的初始化内容,有必要现将记录所有队员信息的plist加入到工程中去,并且读取出来,撰写如下代码
注:每个UITableCell都拥有一个用作重用的ID,上述代码中我们取名叫SimpleTableViewCellId.当在滚动的过程中需要显示新一行时,新行会使用不显示的旧行的Cell对象修改内容后再次显示。当既要显示的Cell跑进“cellForRowAtIndexPath”回调函数的时候,“dequeueReusableCellWithIdentifier”方法返回的就是第一个Cell对象而不再需要新建内存,开发者只需要在第一个Cell上进行内容的重新配置后即可
效果图:
一般,开发者可以将UITableView的datasource和delegate对象设置成同一个控制器对象,delegate的回调函数并非强制实现,如果控制器没有特别实现代理回调函数的话,UITableView将用默认的值代替,比如默认无页眉页脚,默认点击UITableViewCell时不做任何处理等。
但是datasource却不同,想要正常显示一个UITableView,作为datasource的对象需要强制地实现两个回调方法。
-(NSInteger)tableView:(UITableViewView *)tableView numberOfRowsInSection:(NSInteger)section -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
表视图的使用最适合的场景时数据列表,我们以一份恒大足球队的球员名单来做示例,数据存储用苹果程序最喜欢的plist文件格式记录。如下图:
每个球员一共有三个信息:球衣号码,球员名字和角色
在具体工作开展之前,有必要为这个结构声明一个数据模型,代码如下:
HBPlayerInfo.h文件
@interface HBPlayerInfo : NSObject @property (nonatomic,retain)NSString *name; @property (nonatomic,retain)NSString *role; @property (nonatomic,retain)NSNumber *number;
HBPlayerInfo.m文件 1 @implementation HBPlayerInfo @synthesize name=_name; @synthesize role=_role; @synthesize number=_number;
随后在UITableViewControll的子类取名SimpleTabelViewController,对于头文件,声明如下:
//SimpleTableViewContoller.h @interface HBSimpleTableViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate> { //数据源 NSArray *_datasource; } @property (nonatomic ,readonly, retain) NSArray *datasource; //数据源赋值 -(void)initData; //界面配置 -(void)initUI; //UIViewController的标题 -(NSString *)title;
头文件声明了2个初始化的方法,对于数据源的初始化内容,有必要现将记录所有队员信息的plist加入到工程中去,并且读取出来,撰写如下代码
- (void)viewDidLoad { [super viewDidLoad]; // Uncomment the following line to preserve selection between presentations. // self.clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem; self.tableView.dataSource=self; self.tableView.delegate=self; [self initData]; [self initUI]; self.navigationItem.title=[self title]; } -(void)initData { NSMutableArray *arrPlayer=[NSMutableArray arrayWithCapacity:0]; NSArray *arrPlist=nil; //读取工程中的球员信息plist arrPlist=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"hengda" ofType:@"plist"]]; HBPlayerInfo *onePlayer=nil; //将信息挨个解析到数据模型中 for(NSDictionary *onePlayerInfo in arrPlist) { onePlayer=[[HBPlayerInfo alloc]init]; onePlayer.name=[onePlayerInfo objectForKey:@"name"]; onePlayer.role=[onePlayerInfo objectForKey:@"role"]; onePlayer.number=[onePlayerInfo objectForKey:@"number"]; [arrPlayer addObject:onePlayer]; } //数据的赋值 if(_datasource!=nil) { _datasource=nil; } _datasource=[[NSArray alloc]initWithArray:arrPlayer]; } -(void)initUI { self.tableView.allowsSelection=NO; } -(NSString *)title { return @"广州恒大俱乐部"; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { #warning Potentially incomplete method implementation. // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { #warning Incomplete method implementation. // Return the number of rows in the section. return self.datasource.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier=@"SimpleTableViewCellId"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // Configure the cell... if(cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } //配置Cell的内容 HBPlayerInfo *onePlayer=[self.datasource objectAtIndex:indexPath.row]; if (onePlayer) { cell.textLabel.text=onePlayer.name; } //提供UITableView需要的Cell return cell; }
注:每个UITableCell都拥有一个用作重用的ID,上述代码中我们取名叫SimpleTableViewCellId.当在滚动的过程中需要显示新一行时,新行会使用不显示的旧行的Cell对象修改内容后再次显示。当既要显示的Cell跑进“cellForRowAtIndexPath”回调函数的时候,“dequeueReusableCellWithIdentifier”方法返回的就是第一个Cell对象而不再需要新建内存,开发者只需要在第一个Cell上进行内容的重新配置后即可
效果图:
相关文章推荐
- IOS 表视图(UITableVIew)的使用方法(3)名单的索引显示
- IOS 表视图(UITableVIew)的使用方法(2)名单的分段显示
- IOS 表视图(UITableVIew)的使用方法(8)表视图的编辑功能(多选)
- IOS 表视图(UITableVIew)的使用方法(7)表视图的编辑功能(拖拉调整排序位置)
- IOS 表视图(UITableVIew)的使用方法(4)自定义表视图单元
- IOS 表视图(UITableVIew)的使用方法(5)表视图的编辑功能(删除)
- IOS 表视图(UITableVIew)的使用方法(6)表视图的编辑功能(新增Add)
- iOS UITableView reloadData 视图漂移或者闪动解决方法
- iOS 最全的UITableView的各种使用方法
- IOS使用UItableView实现下拉菜单组件(UITableView的使用方法)
- ios--UITableview的基本使用方法
- IOS UITableView表视图和导航控制器的交互使用
- iOS中UITableView使用总结和 iOS UITableView代理方法详解
- iOS中表视图(UITableView)使用详解
- [转]IOS 学习笔记(8) 滚动视图(UIScrollView)的使用方法
- iOS滚动视图UIScrollView使用方法
- iOS中表视图(UITableView)使用详解
- IOS学习笔记(十)之UIImageView图片视图的基本概念和使用方法
- Android基于OpenGL在GLSurfaceView上绘制三角形及使用投影和相机视图方法示例
- IOS学习笔记(十)之UIImageView图片视图的基本概念和使用方法