表视图继承自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. Ext.Net 使用总结之GridPanel中的选中行

    1.判断GridPanel中是否选中了某行 if (!GridPanel1.hasSelection()) { Ext.Msg.alert("提示", "请选择记录!&q ...

  2. Nginx +iis反向代理

    一:简介      Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.由俄罗斯的程序设计师Igor Sysoev所 ...

  3. 用SCMD2.0.8.0汉化版制作OB地图简易教程

    [综合] [复制链接]     Fenix_king       153 主题 0 好友 1万 积分 金星 该用户从未签到 星币 6392 水晶 0 星望 22 精华 0 发消息 电梯直达 楼主   ...

  4. java——%

    %; //结果为1 %; //结果为-1 ; //结果为1.2000000000000002

  5. 关于BFC

    参考  http://www.html-js.com/article/1866(很棒! 还有栗子) http://www.cnblogs.com/lhb25/p/inside-block-format ...

  6. The error indicates that IIS is in 32 bit mode, while this application is a 64 b it application and thus not compatible.

    I was trying to install a new WSS v3 Sharepoint on a 64 bit Windows 2003 server today but the instal ...

  7. IIS启动网站

    在启动一个网站前要先打开一个服务.可是Windows 的提示非常的奇怪: “除非 Windows Activation Service (WAS)和万维网发布服务(W3SVC)均处于运行状态,否则无法 ...

  8. Linux菜鸟之路[4]-cal,date,bc,echo $LANG,man

    由于前四天一直在看鸟哥的linux书本的计算机的一些基础知识,今天才接触基本的命令,从今天起每天记录一下自己的linux学习过程. cal:日历 cal: cal 2015:列出2015年所有日历 c ...

  9. Android SDK与API版本的对应关系

    看教程.开发Android程序等很多地方,需要设置Android SDK的版本,而其要我们写的却是API版本的数字, 为了方便查看 Android SDK与API版本的对应关系 我在SDK Manag ...

  10. HDU 1012 u Calculate e

    题解:直接模拟 #include <cstdio> int main(){ puts("n e");puts("- -----------");pu ...