IOS开发之表视图(UITableView)的基本介绍(一)

(一):UITableView的基本概念

1.在IOS开发中,表视图的应用十分广泛和普及。因此掌握表视图的用法显得非常重要。一般情况下对于数据的展示

我们都会选择表视图,比如通讯录和一些数据列表。

2.我们可以选择创建表视图也可以创建表视图控制器。

(二)UITableView基本样式如下(1:UITableViewStylePlain(普通表视图),2:UITableViewStyleGroup(分组表视图)):

(三)UITableView表视图的结构:首先我们来看一张设计图:

根据上面的图,下面我们来分析一下表视图的结构:

1:表头视图(table header view).表视图最上边的视图,用于展示表视图的信息,例如上面下拉刷新信息。

2:表脚视图(table footer view).表视图最下边的视图,用于展示表视图的部分信息,例如上图加载更多信息。

3:单元格(cell)。它是组成表视图每一行的单位视图,上图一行一行的单元图

4:节(selection)。它是多个单元格在组成,并且有节头和节脚,正如上图蓝色框框的(Group Start)节头,(Group End)节尾

5:节头。节的头部,描述节的信息,例如上图 Group Start.

6:节脚.节的尾部,描述节的信息或者一些声明信息。例如上图 Group End.

(四)UITableView表视图的分类

IOS中表视图主要分为两大类(普通表视图与分组表视图),下面来稍微了解一下这两类表视图

1:普通表视图:主要用于一般表,最常见的是,我们不知道有多少数据需要进行显示.

2:分组表视图:主要用于都一些数据进行布局分成不同的组进行显示。见下面的图,左边为普通表视图,右边为分组表视图;

除此之外,看上面左边的图是带上索引列的(efghijk...),同样还可以加上选择列和搜索栏。

3:下图,给用户提供一个选择的列表,这样使用选择表视图可以代替复选框控件。

下面带了搜索栏如:这样可以在我单元格很多的情况之下,去借助搜索栏进行过滤搜索。搜索栏一般都会放在表头。

需要我们把UITableView上滑倒最顶端才能看到搜索栏。

以上就是先介绍一下表视图(UITableView)包括表视图的基本概念,基本样式,结构以及表视图的分类,下一篇就开始实现表视图并进行深入理解。


IOS开发之表视图(UITableView)的相关类,属性与表视图实现学习(二)

(一)UITableView的相关类解析:

首先我们来看张类的结构图:

1:表视图(UITableView)是继承自UIScrollView,这样就可以使得我们的表视图可以实现上下的滚动。

2:同时表视图(UITableView),还有两个委托①:UITableViewDelegate委托协议,一般我们用来处理表视图的基本样式(例如:单元格的高度等)还有去捕捉选中单元格的事件。②:UITableViewDataSource委托协议,必要要去实现该协议的数据源方法,来完成表视图的数据配置。

3:UITableViewController:是表视图(UITableView)的控制器类。

4:UItableViewCell:是单元格类.

(二):数据源协议和委托源协议介绍:

1:UITableViewDataSource协议:我们去实现其中的方法,来完成我们的表视图的数据配置,从而来显示表视图。其中我们必须要去实现的两个方法如下:

  1. <span style="font-size:18px;">//进行返回每个section(节)中单元格的数量
  2. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
  3. // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
  4. // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
  5. // 为表视图中的单元格创建数据
  6. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;</span>

除了以上的两个必须实现的方法,还有一些以下的可选实现方法:

  1. <span style="font-size:18px;">// 返回section(节)的个数
  2. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;              // Default is 1 if not implemented
  3. //返回section(节)头的标题
  4. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;    // fixed font style. use custom view (UILabel) if you want something different
  5. // 返回section(节)尾的标题
  6. - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
  7. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
  8. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
  9. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;                                                    // return list of section titles to display in section index view (e.g. "ABCD...Z#")
  10. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;
  11. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
  12. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
  13. </span>

2:UITableViewDelegate:协议可以用来设定表视图中的节头与节尾 同时还可以去响应一些点击事件,主要的一些方法如下:

  1. <span style="font-size:18px;">- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;   // custom view for header. will be adjusted to default or specified header height
  2. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;   // custom view for footer. will be adjusted to default or specified footer height
  3. - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
  4. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;</span>

更多方法可以去官网UITableView进行查询。

(三) 表视图(UITableView)的一些常用方法和属性:

1:常用属性:

①:@property(nonatomic) UITableViewCellSeparatorStyle separatorStyle;     默认为UITableViewCellSeparatorStyleSingleLine

②:@property(nonatomic,retain) UIColor               *separatorColor;    默认为:the standard separator gray

③:@property(nonatomic,retain) UIView *tableHeaderView;            头部视图

④:@property(nonatomic,retain) UIView *tableFooterView;      尾部视图

⑤:@property(nonatomic)          CGFloat                    rowHeight;            // 单元格高度

⑥:@property(nonatomic)          CGFloat                    sectionHeaderHeight;  // 头部行高

⑦:@property(nonatomic)          CGFloat                    sectionFooterHeight;    //尾部行高

⑧:@property(nonatomic,readwrite,retain) UIView *backgroundViewNS_AVAILABLE_IOS(3_2);

⑨:@property(nonatomic,readonly) UITableViewStyle           style;

2:常用方法:

①:- (void)reloadData;                // reloads everything from scratch. redisplays visible rows. because we only keep info about visible rows, this is cheap. will adjust offset if table shrinks  刷新单元格的数据

②:- (void)reloadSectionIndexTitlesNS_AVAILABLE_IOS(3_0);  // reloads the index bar.

③:- (NSInteger)numberOfSections;   //返回节的数量

④:- (NSInteger)numberOfRowsInSection:(NSInteger)section;//返回每个节的单元格的数量

⑤:- (CGRect)rectForSection:(NSInteger)section;                                   // includes header, footer and all rows

⑥:- (CGRect)rectForHeaderInSection:(NSInteger)section;

⑦:- (CGRect)rectForFooterInSection:(NSInteger)section;

⑧:- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;

⑨:- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;                        // returns nil if point is outside table

⑩:- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;        //返回指定单元格的NSIndexPath实例

十一:- (NSArray *)indexPathsForRowsInRect:(CGRect)rect;            //返回指定范围的NSIndexPath实例数组

十二:- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;           // returns nil if cell is not visible or index path is out of range     //返回指定NSIndexPath实例的单元格实例

十三:- (NSArray *)visibleCells;  //返回可见的单元格的数组

十四- (NSArray *)indexPathsForVisibleRows;  //返回可见单元格的NSIndexPath实例数组

十五:- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)sectionNS_AVAILABLE_IOS(6_0);

十六:- (UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)sectionNS_AVAILABLE_IOS(6_0);

十七:- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;     //滑动到指定的位置,并且可以加上动画效果

十八:- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

(四)例子实现表格布局

简单的来说:是以下几个步骤:1.配置数据源,2.实现数据源方法,3.设置代理方法。下面来看实例

  1. <span style="font-size:18px;">//
  2. //  ZTTRootViewController.m
  3. //  UITableViewDemo1
  4. //
  5. //  Created by 江清清 on 14-3-19.
  6. //  Copyright (c) 2014年 江清清<<a>http://www.0513.it/</a>中天科技软件技术股份有限公司>. All rights reserved.
  7. //
  8. #import "ZTTRootViewController.h"
  9. #import "ZTTDetailsViewController.h"
  10. #define  kDeviceHeight   [UIScreen mainScreen].bounds.size.height
  11. @interface ZTTRootViewController ()
  12. @end
  13. @implementation ZTTRootViewController
  14. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  15. {
  16. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  17. if (self) {
  18. self.title=@"UITableView Style";
  19. }
  20. return self;
  21. }
  22. -(void)loadView{
  23. UIView *view=[[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame];
  24. //[view setBackgroundColor:[UIColor redColor]];
  25. self.view=view;
  26. [view release];
  27. //开始进行配置数据源
  28. self.listArray=@[@"UITableViewStylePlain",@"UITableViewStyleGrouped"];
  29. _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320,kDeviceHeight-20-44) style:UITableViewStylePlain];
  30. //实现数据源方法
  31. [_tableView setDataSource:self];
  32. //设置点击事件 代理方法
  33. [_tableView setDelegate:self];
  34. [self.view addSubview:_tableView];
  35. }
  36. - (void)viewDidLoad
  37. {
  38. [super viewDidLoad];
  39. // Do any additional setup after loading the view.
  40. }
  41. - (void)didReceiveMemoryWarning
  42. {
  43. [super didReceiveMemoryWarning];
  44. // Dispose of any resources that can be recreated.
  45. }
  46. #pragma mark- tableview date source
  47. /*
  48. * 一个selection中又多少个单元格
  49. */
  50. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  51. {
  52. return  [_listArray count];
  53. }
  54. // indexPath
  55. //创建单元格
  56. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  57. static NSString *cellInditifier=nil;
  58. // 创建单元格对象
  59. UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellInditifier];
  60. if(cell ==nil){
  61. cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellInditifier]autorelease];
  62. }
  63. NSString *text=[self.listArray objectAtIndex:indexPath.row];
  64. cell.textLabel.text=text;
  65. return  cell;
  66. }
  67. // 表视图中有几个selection
  68. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  69. return  1;
  70. }
  71. // 选中单元格的方法
  72. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  73. NSLog(@"didSelect");
  74. //进行跳转到相应的页面
  75. ZTTDetailsViewController *detailsVC=[[ZTTDetailsViewController alloc]init];
  76. detailsVC.isPlain=indexPath.row==0?YES:NO;
  77. [self.navigationController pushViewController:detailsVC animated:YES];
  78. [detailsVC release];
  79. }
  80. -(void)dealloc{
  81. [_tableView release];
  82. _tableView=nil;
  83. [super dealloc];
  84. }
  85. @end
  86. </span>

运行截图如下:

3:上面的代码例子是一般的表格,如果我们要表格中加入表头(header)和表尾(footer)话,我们需要实现以下两个数据源方法:

  1. <span style="font-size:18px;">- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;    // fixed font style. use custom view (UILabel) if you want something different
  2. - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;</span>

运行截图如下:

IOS开发之表视图(UITableView)的更多相关文章

  1. iOS开发之表视图爱上CoreData

    在接触到CoreData时,感觉就是苹果封装的一个ORM.CoreData负责在Model的实体和sqllite建立关联,数据模型的实体类就相当于Java中的JavaBean, 而CoreData的功 ...

  2. IOS开发之表视图添加索引

    我们要实现的效果如下. 1.修改ControlView.h,即添加变量dict,用于存储TabelView的数据源. #import <UIKit/UIKit.h> @interface  ...

  3. IOS 表视图UITableView 束NSBundle

    今天搞了一下表视图UITableView 表视图是在以后应用程序开发中经常用到的一个视图,所以必须要熟练掌握 所获不多,对视图有了一个大概的了解 其中有用到NSBundle , 束   这个类 先说一 ...

  4. iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)

    iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一) 一.项目结构和plist文件 二.实现代码 1.说明: 主控制器直接继承UITableViewController // ...

  5. iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建

    iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建 一.实现效果 说明:该示例在storyboard中使用动态单元格来完成. 二.实现 1.项目文件结构 ...

  6. 视图交互--表视图(UITableView)的cell交互析略

    在表视图UITableView的cell上经常有一些交互,根据项目开发中的情况,需要对此进行一些规范.总结出了几种交互方法,这些方法在其他视图的交互上同样可以适用.用一个简单的例子来举例说明一下,其他 ...

  7. iOS开发UI篇—实现UItableview控件数据刷新

    iOS开发UI篇—实现UItableview控件数据刷新 一.项目文件结构和plist文件 二.实现效果 1.说明:这是一个英雄展示界面,点击选中行,可以修改改行英雄的名称(完成数据刷新的操作). 运 ...

  8. 表视图(UITableView)与表视图控制器(UITableViewController)

    表视图(UITableView)与表视图控制器(UITableViewController)其实是一回事. 表视图控制器是一种只能显示表视图的标准视图控制器,可在表视图占据整个视图时使用这种控制器.虽 ...

  9. iOS开发高级分享 - iOS的可折叠表视图

    导言 我曾经开发过一个iphone应用程序,它显示了大量的输入,这些输入分为不同的类别,在`UITableView`...若要更改其中一个输入的值,用户按下表视图中的对应行,并在出现的单独屏幕中更改该 ...

随机推荐

  1. C#程序员整理的Unity 3D笔记(十三):Unity 3D基于组件的思想

    如果你接触过<设计模式>.软件架构的编程思想,就会知道优秀的设计准则:“组合优于继承的”. 这句话很简短,但开始学习OOP的时候,真切的是—-不太好理解(以我个人当初学习为例). OOP的 ...

  2. OC6-网址分割

    // // HtmlManger.h // OC6-网址分割 // // Created by qianfeng on 15/6/17. // Copyright (c) 2015年 qianfeng ...

  3. CSS 初体验之一

    层叠样式表 (Cascading Style Sheets)”,它主要是用于定义HTML内容在浏览器内的显示样式. 1.首先先看盒装模型,如下图,平时设置的width,height是指元素内容(con ...

  4. POJ 3928 Ping pong

    题目链接:http://poj.org/problem?id=3928 乒乓比赛,有N个人参加,输入每个玩家的技能等级,对每个人设置一个特定ID和一个技能值,一场比赛需要两个选手和一个裁判,只有当裁判 ...

  5. 韩顺平细说Servlet视频系列之tom相关内容

    韩顺平细说Servlet视频系列之tom相关内容 tomcat部署项目操作(注意:6.0版本以后的支持该操作,5.x版本需要另外配置?待验证!) 项目发布到tomcat的webapps文件下,然后启动 ...

  6. rpm与yum命令的初步认识

    RPM:Red Hat package manager(RedHat软件包管理工具),现在为RPM is Package Manager好比windows里的文件扩展名为·exe的软件包. RPM的包 ...

  7. [分享]好用的Markdown编辑器

  8. ubuntu server版连接vpn服务器

    命令行的方法: 1.要下载pptp的客户端    sudo apt-get install pptp-linux 2.创建连接     sudo pptpsetup –create vpn001 –s ...

  9. PHP获取搜索引擎关键字来源(百度、谷歌、雅虎、搜狗、搜搜、必应、有道)

    <?php //获取来自搜索引擎入站时的关键词 function get_keyword($url,$kw_start) { $start=stripos($url,$kw_start); $u ...

  10. CCNP第四天 OSPF综合实验(1)

    ospf综合实验(1) 本实验主要考察ospf中的接口上的多种工作方式 实验如图所示: 所用拓扑为CCNP标准版,如图: --------------------------------------- ...