表视图继承自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)表视图的示例的更多相关文章

  1. IOS 表视图(UITableVIew)的使用方法(6)表视图的编辑功能(新增Add)

    表视图的新增功能和删除功能虽然目的不同,但是工作流程是相似的 下面列出在处理新增的回调函数时,与删除所不同的逻辑部分代码. 显示下过如下: #pragma mark #pragma mark Tabl ...

  2. IOS 表视图(UITableVIew)的使用方法(8)表视图的编辑功能(多选)

    在表视图的删除操作中,每次只能够对其中一个单元进行删除,如果想要同时删除多条记录,不得不挨个地进行标准的删除操作 所以如果能够实现多选的机制,无论是删除还是其他功能的嫁接,都会变得更加方便 当UITa ...

  3. IOS 表视图(UITableVIew)的使用方法(5)表视图的编辑功能(删除)

    默认的,如果表视图支持编辑,那用户可以通过两种方式来删除某些行,其一为单击左侧的红色按钮后行右侧显示“Delete”按钮,其二为在单元行上的手指向左滑动,“Delete”按钮也会出现供用户单击.无论哪 ...

  4. IOS 表视图(UITableVIew)的使用方法(7)表视图的编辑功能(拖拉调整排序位置)

    除了每个单元行左边的删除和新增图标,UITableView还支持在单元行的右侧显示一个供用户拖拉调整排序位置的控件. 不过如果要显示此控件,UITableView的数据源需要实现以下的方法. -(vo ...

  5. IOS 表视图(UITableVIew)的使用方法(3)名单的索引显示

    当数据量特别大时,简单地以role进行分段,对实际查找的效率提升并不大.就像上一节开头所说,开发者可以根据球员名字的首字母进行分段,且分成26段.由于段数较多,可以使用UITableView的索引机制 ...

  6. IOS 表视图(UITableVIew)的使用方法(2)名单的分段显示

    我们可以采用名字分段法,名字分段会在之后的小节中显示,这是转而使用球员的角色分段发,以最直接的入手点讲解好UITableView的分段使用方法.本节示例时基于上节的SimpleTableViewCon ...

  7. IOS 表视图(UITableVIew)的使用方法(4)自定义表视图单元

    UITableViewCell的自定义往往需要自建一个UITableViewCell的子类后进行作业.开发者可以选择通过xib或者直接在UITableViewCell的布局中进行UITableView ...

  8. iOS开发UITableView基本使用方法总结

    本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...

  9. iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏

    本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...

随机推荐

  1. 弹出窗口a标签写下载,再弹出窗口

    如果这个窗口是弹出出口,直接<a href="">点击下载<a>是不行的,得用js这样写,弹出并关闭,不然会回到首页,如果没有定义首页会报错,<a h ...

  2. xmanager 使用

    linux 上安装xterm windows上启动命令: /usr/bin/xterm -ls -display $DISPLAY

  3. sql查询当天数据

    向数据库中添加日期 MS SQL SERVER: NSERT into student(studentid,time1)values('15',getdate()); MY SQLinsert int ...

  4. 原生js判断某个元素是否有指定的class名的几种方法

    [注意]以下方法只对class只有一个值的情况下操作 ************************************************************* 结构部分: <d ...

  5. 无法连接远程mysql问题

    按照别人的博客操作之后,重启了服务: 前几天,windows上的mysql无法远程连接,在网上搜了一下,都是一个说法,神马改表.加权限.刷新等等,尝试之后都没有用,后来同事告诉了我一个方法,就解决了那 ...

  6. STL模板_容器概念

    一.STL(Standard Template Library,标准模板库)概述1.容器:基于泛型的数据结构.2.算法:基于泛型的常用算法.3.迭代器:以泛型的方式访问容器中的元素,是泛型的算法可以应 ...

  7. 关于XPath的基本语法

    关于XPath基础语法 关于XPath基础语法 更详细的请看: XPath语法 XPath 使用路径表达式来选取 XML 文档中的节点或节点集.节点是通过沿着路径 (path) 或者步 (steps) ...

  8. mysql-connector-c 安装

    cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/usr/local make make install

  9. PL/SQL database character set(AL32UTF8) and Client character set(ZHS16GBK) are different

    启动PL/SQL Developer 报字符编码不一致错误 Database character set (AL32UTF8) and Client character set (ZHS16GBK) ...

  10. 设置cmd的codepage的方法

    设置cmd的codepage的方法 有时候,我们的cmd.exe的codepage和字体等会变化,比如突然由中文变成英文的codepage(因为一些sh程序的干扰). 下面是修正方法: [HKEY_C ...