iOS 表视图(UITableVIew)的使用方法(1)表视图的示例
表视图继承自UIScrollView,所以有着大多UIScrollView的操作特性,诸如手指控制内容的滚动,内容视图到顶端或者低端时的自动反弹等。配合UINavigationController的导航特性,表视图可以将大量有一定规则顺序的数据,完整的呈现到客户端上。
一般,开发者可以将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:];
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 ;
} - (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)的使用方法(1)表视图的示例的更多相关文章
- IOS 表视图(UITableVIew)的使用方法(6)表视图的编辑功能(新增Add)
表视图的新增功能和删除功能虽然目的不同,但是工作流程是相似的 下面列出在处理新增的回调函数时,与删除所不同的逻辑部分代码. 显示下过如下: #pragma mark #pragma mark Tabl ...
- IOS 表视图(UITableVIew)的使用方法(8)表视图的编辑功能(多选)
在表视图的删除操作中,每次只能够对其中一个单元进行删除,如果想要同时删除多条记录,不得不挨个地进行标准的删除操作 所以如果能够实现多选的机制,无论是删除还是其他功能的嫁接,都会变得更加方便 当UITa ...
- IOS 表视图(UITableVIew)的使用方法(5)表视图的编辑功能(删除)
默认的,如果表视图支持编辑,那用户可以通过两种方式来删除某些行,其一为单击左侧的红色按钮后行右侧显示“Delete”按钮,其二为在单元行上的手指向左滑动,“Delete”按钮也会出现供用户单击.无论哪 ...
- IOS 表视图(UITableVIew)的使用方法(7)表视图的编辑功能(拖拉调整排序位置)
除了每个单元行左边的删除和新增图标,UITableView还支持在单元行的右侧显示一个供用户拖拉调整排序位置的控件. 不过如果要显示此控件,UITableView的数据源需要实现以下的方法. -(vo ...
- IOS 表视图(UITableVIew)的使用方法(3)名单的索引显示
当数据量特别大时,简单地以role进行分段,对实际查找的效率提升并不大.就像上一节开头所说,开发者可以根据球员名字的首字母进行分段,且分成26段.由于段数较多,可以使用UITableView的索引机制 ...
- IOS 表视图(UITableVIew)的使用方法(2)名单的分段显示
我们可以采用名字分段法,名字分段会在之后的小节中显示,这是转而使用球员的角色分段发,以最直接的入手点讲解好UITableView的分段使用方法.本节示例时基于上节的SimpleTableViewCon ...
- IOS 表视图(UITableVIew)的使用方法(4)自定义表视图单元
UITableViewCell的自定义往往需要自建一个UITableViewCell的子类后进行作业.开发者可以选择通过xib或者直接在UITableViewCell的布局中进行UITableView ...
- iOS开发UITableView基本使用方法总结
本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...
- iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏
本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...
随机推荐
- Java SSH框架学习(入门)
SSH就是 struts+spring+hibernate 的一个集成框架,是java中一种流行的JAVA WEB 应用程序开源框架.由于我最熟悉的还是ASP.NET的的MVC和Python的Djan ...
- 引入css ,使用@import和link的方式
我们也经常听到有人说要使用link来引入CSS更好,但是你知道为什么吗? 继续往下看 linklink就是把外部CSS与网页连接起来. @importimport文字上与link的区别就是它可以把在一 ...
- CSS:margin负数的使用
给所有div加上边框=10px之后,再给所有div设置margin-left与margin-top;以及浮动(float:left) 因此时需要鼠标悬停效果:所以设置给div设置伪类:hover,然因 ...
- SSH框架——Sprign声明式事务
Spring事务管理 Spring是SSH中的管理员,负责管理其它框架,协调各个部分的工作.今天一起学习一下Spring的事务管理.Spring的事务管理分为声明式跟编程式.声明式就是在Spring的 ...
- 浏览器中输入Google.com然后按下回车键
按下回车键,当然会产生操作系统的中断响应,产生一个WM_KEYDOWN消息,当然这些都不是计算机网络的东西,这里只讨论计算机网络相关的东西: 解析URL 浏览器通过URL能够知道下面的信息: Prot ...
- linux install nginx error
1 2 3 4 5 6 7 8 9 10 11 [mahao01@127.0.0.1 nginx-1.2.9]$ make make -f objs/Makefile make[1]: Enterin ...
- day7_python学习笔记_chapter9_文件
1. open(), file(), 作用完全相同 2. 语法: file_object = open(file_name, access_mode='r', buffering='-1') acce ...
- poj 1321 棋盘问题 递归运算
棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19935 Accepted: 9933 Description ...
- 【转】杭电ACM试题分类
注:网上搜的 第一篇 1001 这个就不用说了吧1002 简单的大数1003 DP经典问题,最大连续子段和1004 简单题1005 找规律(循环点)1006 感觉有点BT的题,我到现在还没过1007 ...
- ObjectiveC 文件操作一
1,引用和使用文件 NSFileManager 是一个单例对象,在mac应用中可以获取任何地址,在IOS中获取的是相对应的应用程序的地址.可以使用 defaultManager 来得到当前应用程序地址 ...