UITableView基本用法
表用于显示数据列表,数据列表中的每一项都由行表示。IOS没有行的限制,行数仅受可用才存储空间的限制,IOS的表只有一列。
表视图是显示表数据的试图对象,是UITableView类的一个实例,表中的每个可建行都有UITableViewCell类实现。即一个UITableView实例由若干UITableViewCell组成。
表视图不负责存储表中的所有数据,只存储足够绘制当前可见行的数据。每次只加载一屏幕的数据。表视图从遵循UITableViewDelegate协议的对象中获取配置数据,从遵循UITableViewDataSource协议的对象中获得行数据。
(1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
这个方法返回 tableview 有多少个section
- //返回有多少个Sections
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return array.Count; //默认值为1
- }
(2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;
这个方法返回对应的section有多少个元素,也就是多少行。
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return 10; //默认也是1 ,分区中的行的个数
- }
(3)-(UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
返回指定的row 的cell。这个地方是比较关键的地方,一般在这个地方来定制各种个性化的 cell元素。这里只是使用最简单最基本的cell 类型。
其中有一个主标题cell.textLabel 还有一个副标题cell.detailTextLabel, 还有一个 image在最叫cell.imageView. 还可以设置右边的图标,通过cell.accessoryType 可以设置是饱满的向右的蓝色箭头,还是单薄的向右箭头,还是勾勾标记。
- //设置每行调用的cell
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";//自定义的标识符
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
- SimpleTableIdentifier];
- if (cell == nil) {
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
- reuseIdentifier: SimpleTableIdentifier] autorelease];
- }
- cell.imageView.image=image;//未选cell时的图片
- cell.imageView.highlightedImage=highlightImage;//选中cell后的图片
- cell.text=@”测试文本”;
- return cell;
- }
表中的每一行都代表一个UITableViewCell。可以使用图像、文本还有辅助的图标等来自定义你自己的UITableViewCell。你可以自定义你自己的cell如下模型或者像appstore那样的。
(4)- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
返回指定的section的 header的title,如果这个sectionheader 有返回view,那么title就不起作用了。
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
- {
- if (tableView == tableView_)
- {
- if (section == 0)
- {
- return @"Girls";
- }
- else
- {
- return @"Boys";
- }
- }
- }
(5)设置让UITableView行缩进
- // 把每一行的缩进级别设置为其行号,第一行为1,第二行为2
- -(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
- NSUInteger row = [indexPath row];
- return row;
- }
(6)设置cell每行间隔的高度
- - (CGFloat)tableView:(UITableView *)tableView eightForRowAtIndexPath:(NSIndexPath *)indexPath{
- return 40;
- }
(7)设置选中Cell的响应事件
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
- [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
- }
该方法是选中之后执行。另一个方法willSelectRowAtIndexPath是在一行选择前调用,通常用来阻止某行的能否被选中
- -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if(row == 0)
- return nil;//设置第一个可见行不能被选中
- return indexPath;
- }
表用于显示数据列表,数据列表中的每一项都由行表示。IOS没有行的限制,行数仅受可用才存储空间的限制,IOS的表只有一列。
表视图是显示表数据的试图对象,是UITableView类的一个实例,表中的每个可建行都有UITableViewCell类实现。即一个UITableView实例由若干UITableViewCell组成。
表视图不负责存储表中的所有数据,只存储足够绘制当前可见行的数据。每次只加载一屏幕的数据。表视图从遵循UITableViewDelegate协议的对象中获取配置数据,从遵循UITableViewDataSource协议的对象中获得行数据。
(1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
这个方法返回 tableview 有多少个section
- //返回有多少个Sections
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return array.Count; //默认值为1
- }
(2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;
这个方法返回对应的section有多少个元素,也就是多少行。
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return 10; //默认也是1 ,分区中的行的个数
- }
(3)-(UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
返回指定的row 的cell。这个地方是比较关键的地方,一般在这个地方来定制各种个性化的 cell元素。这里只是使用最简单最基本的cell 类型。
其中有一个主标题cell.textLabel 还有一个副标题cell.detailTextLabel, 还有一个 image在最叫cell.imageView. 还可以设置右边的图标,通过cell.accessoryType 可以设置是饱满的向右的蓝色箭头,还是单薄的向右箭头,还是勾勾标记。
- //设置每行调用的cell
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";//自定义的标识符
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
- SimpleTableIdentifier];
- if (cell == nil) {
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
- reuseIdentifier: SimpleTableIdentifier] autorelease];
- }
- cell.imageView.image=image;//未选cell时的图片
- cell.imageView.highlightedImage=highlightImage;//选中cell后的图片
- cell.text=@”测试文本”;
- return cell;
- }
表中的每一行都代表一个UITableViewCell。可以使用图像、文本还有辅助的图标等来自定义你自己的UITableViewCell。你可以自定义你自己的cell如下模型或者像appstore那样的。
(4)- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
返回指定的section的 header的title,如果这个sectionheader 有返回view,那么title就不起作用了。
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
- {
- if (tableView == tableView_)
- {
- if (section == 0)
- {
- return @"Girls";
- }
- else
- {
- return @"Boys";
- }
- }
- }
(5)设置让UITableView行缩进
- // 把每一行的缩进级别设置为其行号,第一行为1,第二行为2
- -(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
- NSUInteger row = [indexPath row];
- return row;
- }
(6)设置cell每行间隔的高度
- - (CGFloat)tableView:(UITableView *)tableView eightForRowAtIndexPath:(NSIndexPath *)indexPath{
- return 40;
- }
(7)设置选中Cell的响应事件
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
- [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
- }
该方法是选中之后执行。另一个方法willSelectRowAtIndexPath是在一行选择前调用,通常用来阻止某行的能否被选中
- -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if(row == 0)
- return nil;//设置第一个可见行不能被选中
- return indexPath;
- }
图片说明:此图片是从其他网站上戒掉的部分图。和上述代码运行效果不大相同。
本文转自HDDevTeam 51CTO博客,原文链接:http://blog.51cto.com/hddev/917290,如需转载请自行联系原作者
UITableView基本用法相关推荐
- RxSwift之UI控件UITableView扩展的使用
一.基本使用 ① 单分区的表格 如下所示,单个分区的表格展示: 示例代码: import UIKit import RxSwift import RxCocoaclass ViewController ...
- UICollectionView框架总结
一.UIcollectionView介绍 1.1.简介 首先看苹果官方文档 UICollectionView Class Reference 的介绍: The UICollectionView cla ...
- 这个我过滤概述UIPickerView键盘处理
一.介绍UIPickView和UIDatePicker(了解) 1.UIPickView什么时候用? 通常在注册模块,当用户需要选择一些东西的时候,比如说城市,往往弹出一个PickerView给他们选 ...
- UICollectionView基本使用详解(OC)
概述 UICollectionView是从iOS6开始引入使用的,目前应用非常广泛,很牛逼!老外的博客也是这么说的(传送门) ## 与UITableView的初步比较 UITableView应该是大家 ...
- iOS开发框架CollectioniOS
CollectioniOS聚合了项目搭建的一些基本模块,节约开发者时间,协助项目的快速搭建,能够满足一个项目的基本实现. github地址:https://github.com/usernameyan ...
- ios开发之--UITableView中的visibleCells的用法
先上图: 具体代码如下: #import "ViewController.h"@interface ViewController ()<UITableViewDelegate ...
- 简述UITableView的属性和用法
UITableView内置了两种样式:UITableViewStylePlain,UITableViewStyleGrouped <UITableViewDataSource,UITableVi ...
- 初级篇第六期:学习UITableView
学习建议:自己动手,丰衣足食 学习周期:1周 学习目的:熟练使用Obejct-C中最常用的控件之一UITableView 学习答疑:欢迎来技术群里提问并做分享 学习工具:Xcode开发环境 学习内容: ...
- (0074)iOS开发之UITableView的优化
写的很好引用 https://www.jianshu.com/p/af6b095aaaf3 前言 这篇文章对 UITableView 的优化主要从以下3个方面分析: 基础的优化准则(高度缓存, cel ...
最新文章
- 让你的博士经历更加轻松愉快的10个tips
- [java]The String Pool
- WEP自动破解工具wesside-ng
- 海思Hi3559A平台移植 opencv4.0.0
- 开发日记-20190521 关键词 bash的几个初始化文件
- Chrome 技术篇-常用web调试技巧清除缓存并硬性重新加载
- matlab摄像头录像保存在哪里,matlab连接摄像头读取视频部分解释
- conda安装cuda_记一次在 RTX 3090 上安装 APEX
- group by 多个字段
- 【AHOI2009】【BZOJ1798】Seq 维护序列seq(线段树模板,易错提醒)
- Solr的原理及使用
- 基于Web的svg编辑器(2)——层次结构设计(DOM结构)
- linux设备开发详解宋宝华,[Linux设备驱动开发详解(第2版)].宋宝华.pdf
- AutoJS实现微信自动聊天机器人
- 根据时间经纬度高程计算天顶角
- 陕西省本级城镇企业退休人员 - 人脸识别APP资格认证操作指南
- 2021最新Java面试真题解析!java开发技能掌握
- 他一定幸福地生活在那里
- 163免费企业邮箱服务地址
- 物联网:断点续传策略及流程图
热门文章
- 架构之技术复杂度与业务复杂度
- java面相对象的关键字_Java面向对象关键字有哪些?
- Docker安装QuestDB教程
- CentOS7安装Nginx,全网最快安装教程
- 项目管理需要使用到的图表
- Soul 网关源码阅读(一) 概览
- oracle: unrecognized service,打开Linux ftp服务,如:vsftpd: unrecognized service
- java pfx提取私钥加签,详解pfx证书提取公私钥的方法
- mysql返回对象_使用mysql_fetch_object()以对象的形式返回查询结果
- 如何给数组用fill函数和memset函数给数组赋初值