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 ...
随机推荐
- New Relic——手机应用app开发达人的福利立即就到啦!
HiWork集成的第三方服务(机器人)将有新的添加啦,添加了BitBucket和New Relic.分别做下介绍啦! 1.BitBucket BitBucket 是一家源码托管站点.採用Mercuri ...
- C++ 清空消息队列
在button等被禁用后,可能须要它在禁用期间不去响应不论什么消息. 能够使用以下的语句片段: MSG msg; //消耗掉消息队列中的全部消息 while(::PeekMessage(&ms ...
- 加入功能区buttonRibbon Button到SP2010特定列表或库
加入功能区button到SP2010某一列表或库 有时候你须要给列表/库的功能区加入新button--没有什么比这更简单的了. 你仅仅须要新建一个SP项目.加入一个feature,加入 ...
- C#反射调用程序集类中方法
建立类 class OperatorClass { /// <summary> /// 加法 /// </summary> /// <param name="x ...
- iOS 根据图片URL从本地相册获取图片
最近做一个聊天的项目,需要发送图片后读取本地图片显示到列表里.刚开始的时候,天真的认为可以用SDWebImage直接加载,然后并不能行. 于是在网上搜了搜,如何根据从相册获取的UIImagePicke ...
- linux 分割文件
import os import sysimport subprocess if len(sys.argv)<3 : print 'usage: filenum filename' file_n ...
- 创建以及加载模块【nodejs第四篇】
建立两个文件,文件一createModule.js ,文件二main.js createModule.js的代码,主要用于创建一个模块 /** * Created by Administrator o ...
- 用原生JS写移动动画案例及实际应用
js很强大 相信很多人都知道,那么它有哪些强大之处呢?有兴趣的人可以去查查,这里就不赘述了,因为不在本片文章讨论的范围. 我们要讲的是怎么用原生JS写移动动画?我们先举一个最简单的动画例子,很多网站的 ...
- 计算Date间的分钟数
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm"); Date date_begin = simpl ...
- BZOJ 1812: [Ioi2005]riv( 树形dp )
树背包, 左儿子右兄弟来表示树, dp(x, y, z)表示结点x, x的子树及x的部分兄弟共建y个伐木场, 离x最近的伐木场是z时的最小代价. 时间复杂度O(N^2*K^2) ----------- ...