表视图在IOS中的应用非常广泛,常用于展示显示数据列表。

在工具组中包含了Table View 和Table View Cell

每个表示图都是UITableView的一个实例,每个可见行都是UITableViewCelll的一个实例。也就是把TableViewCell放入TableView里面。

表格有两种style

分组和列表

分组:Grouped

列表:Plain

下面做一个默认style的表视图实例:

1.先拉出一个TableView到xib中。

2.连接添加的TableView和ViewController

因为IOS开发是使用MVC模式,所以这里要把视图和控制层关联起来,而他们之间的桥梁就是File's Owner.这样一来控制器类就成了TableView的数据源和委托

3.向ViewController.h中添加

@property (nonatomic,retain) NSArray *list;

并使用

<UITableViewDelegate,UITableViewDataSource>协议

向ViewController.m添加实现

@synthesize list=_list;

4.建立数据

在ViewController.m中添加

- (void)viewDidLoad
{[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.NSArray *array = [[NSArray alloc] initWithObjects:@"美国", @"菲律宾",@"黄岩岛", @"中国", @"泰国", @"越南", @"老挝",@"日本" , nil];self.list = array;}- (void)viewDidUnload
{[super viewDidUnload];// Release any retained subviews of the main view.self.list = nil;
}

实现协议的三个方法,这三个方法必须实现不然后果很严重!!!

//绑定数据源
- (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *TableSampleIdentifier = @"TableSampleIdentifier";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];if (cell == nil) {cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:TableSampleIdentifier];}NSUInteger row = [indexPath row];cell.textLabel.text = [self.list objectAtIndex:row];return cell;
}
//是否分组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{return 1;
}
//行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{return self.list.count;
}

运行结果:

这样一个简单的tableView例子就完成了 NICE!

5.为每一行加图片,首先添加图片文件到到项目中

修改方法:- (UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath;

  UIImage *img= [UIImage imageNamed:@"a.png"];cell.imageView.image=img;
- (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *TableSampleIdentifier = @"TableSampleIdentifier";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];if (cell == nil) {cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:TableSampleIdentifier];}  UIImage *img= [UIImage imageNamed:@"a.png"];cell.imageView.image=img;NSUInteger row = [indexPath row];cell.textLabel.text = [self.list objectAtIndex:row];return cell;
}

效果如下:

6.设置表格的样式,说明文字

initWithStyle:UITableViewCellStyleSubtitle
cell.detailTextLabel.text =@"nice";
- (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *TableSampleIdentifier = @"TableSampleIdentifier";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];if (cell == nil) {cell = [[UITableViewCell alloc]        initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:TableSampleIdentifier];}UIImage *img= [UIImage imageNamed:@"a.png"];cell.imageView.image=img;NSUInteger row = [indexPath row];cell.textLabel.text = [self.list objectAtIndex:row];    cell.detailTextLabel.text =@"nice";return cell;
}

效果如下:

7.设置缩进级别

//缩进级别
-(NSInteger) tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{NSUInteger row = [indexPath row];return row;
}

效果如下:

那么一行会按照它的索引去增加缩进,nice~

8.处理行的选择

可以使用-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath这个方法来做处理

//行的选择
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{NSString *rowString = [self.list objectAtIndex:[indexPath row]];UIAlertView * alter = [[UIAlertView alloc] initWithTitle:@"选中的行信息" message:rowString delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];[alter show];
}

效果如下:

9.设置字体的大小和行高

cell.textLabel.font = [UIFont boldSystemFontOfSize:50];
- (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *TableSampleIdentifier = @"TableSampleIdentifier";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];if (cell == nil) {cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:TableSampleIdentifier];}UIImage *img= [UIImage imageNamed:@"a.png"];cell.imageView.image=img;NSUInteger row = [indexPath row];cell.textLabel.text = [self.list objectAtIndex:row];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:50];cell.detailTextLabel.text =@"nice";return cell;
}

效果如下:

但这样我们又发现行高不对,所以我们需要在控制层类里面再添加一个委托方法来解决这个问题,

//委托解决行高问题
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{return 70;
}

效果如下:

那么今天的TableView表视图基础知识就学习到这里。

以下是IOS开发-我的第一个IOS程序及IOS开发-TableView表视图基础 源码:

源码下载

IOS开发-TableView表视图基础相关推荐

  1. IOS开发-TableView表视图LV2

    在上一章节IOS开发-TableView表视图基础的学习后, 我觉得对于表视图的学习不应只局限于基础知识的学习,应用在实战中的话想要构建丰富的多元化视图界面我想还是必须深入地再学习下. 于是有了这个L ...

  2. IOS开发之表视图(UITableView)

    IOS开发之表视图(UITableView)的基本介绍(一) (一):UITableView的基本概念 1.在IOS开发中,表视图的应用十分广泛和普及.因此掌握表视图的用法显得非常重要.一般情况下对于 ...

  3. iOS开发指南:从零基础到App Store上架(第2版 )

    第一部分 基础篇 第1章 开篇综述 2 1.1 iOS概述 2 1.1.1 iOS介绍 2 1.1.2 iOS 6新特性 2 1.2 开发环境及开发工具 3 1.3 本书中的约定 4 1.3.1 案例 ...

  4. iOS开发中正则表达式的基础使用

    正则表达式?什么是正则表达式? 百度百科给出的解释是这样的:正则表达式使用单个字符串来描述.匹配一系列符合某个句法规则的字符串. 根据我的学习,我理解的正则表达式是:一个字符串,这个字符串用来描述我们 ...

  5. [绍棠] iOS开发中正则表达式的基础使用

    正则表达式?什么是正则表达式? 百度百科给出的解释是这样的:正则表达式使用单个字符串来描述.匹配一系列符合某个句法规则的字符串. 根据我的学习,我理解的正则表达式是:一个字符串,这个字符串用来描述我们 ...

  6. IOS 创建简单表视图

    创建简单表视图 此实例主要实现UITableViewDataSource协议中必需要实现的两个方法tableView:numberOfRowsInSection: 和tableView:cellFor ...

  7. 自学IOS开发第3天·基础SwiftUI之动态滑动列表(上)

    文章目录 基础SwiftUI之动态滑动列表 UI构建 创建模型 BlogerData.swift 创建 JSON文件 创建 Model.swift 附稿 基础SwiftUI之动态滑动列表 我完全跟着S ...

  8. IOS开发笔记2-C语言基础复习

    转载请标明出处: http://blog.csdn.net/hai_qing_xu_kong/article/details/53439812 本文出自:[顾林海的博客] 前言 在正式进入ios开发前 ...

  9. 从零开始的iOS开发:00 | Swift基础语法(上)

    目录 一.开发环境 二.关于Swift (一)Swift简介 (二)Swift特性 (三)结语 三.Swift基础语法 (一)编程准备 (二)Hello,world! (三)简单值 1.变量与常量 2 ...

最新文章

  1. CentOS7(64位)安装NVIDIA显卡驱动和CUDA8.0
  2. 明清美文四卷本(共四册)
  3. Date Picker控件:
  4. 本地连接和音量图标显示
  5. STL13-list容器(链表)
  6. bat脚本保存dir结果_MySQL备份脚本,应该这么写
  7. 极简代码(五)—— 斐波那契数列
  8. 服务器安装固态硬盘的步骤,电脑安装固态硬盘及安装系统的详细教程
  9. visual_studio2013、vs2013下载
  10. 火山安卓解析某盘直连
  11. mysql 中一个表里有父子关系_SQLAlchemy - 同一个表中的父子关系
  12. 小技巧|H5禁止手机虚拟键盘弹出
  13. 公开课丨大厂前端初级到高级技术体系
  14. basic计算机编程基础,计算机编程基础(Visual Basic)
  15. 关于Windows分盘的操作
  16. 第五章总结(创建与使用视图)
  17. VS2019中字符串函数的使用
  18. 增广拉格朗日函数法(ALM)
  19. 23个经过时间考验的应用程序,可以管理您的远程软件开发团队
  20. 微信小程序富文本解析 rich-text 、wxParse、mp-html

热门文章

  1. python制作印刷体数据集:数字符号数据集(字符串转图片)
  2. 壳体花纹怎么设计_换热器设计大全
  3. mvc% html.%,MVC的html.doc
  4. mysql 自定义排序函数_MySQL自定义排序函数FIELD()
  5. sqlite3 可视化工具 linux,【SQLite3下载】SQLite3可视化工具 v3 3.27.2 官方版-开心电玩...
  6. centos java tar_CentOS安装JDK-tar.gz文件
  7. antd vue关闭模态对话框_我不能没有的5个Vue.js库
  8. 如何调整金格电子章服务器印章_电子签章赋能勘察设计新动力
  9. 正确率能很好的评估分类算法吗_机器学习算法:分类知识超全总结!
  10. WebDriver API学习记录