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

2023-11-13

  iOS学习笔记(十二)之IOS开发之表视图(UITableView)的讲解与使用(二)(博客地址:http://blog.csdn.net/developer_jiangqq)转载请注明地址.

      Author:hmjiangqq

      Email:jiangqqlmj@163.com

   上一篇初步学习了一下表视图(UITableView)的基本概念内容(点击进入),今天就表视图的其他知识进行学习,并且实现表视图。

 (一)UITableView的相关类解析:

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

    

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

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

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

   4:UItableViewCell:是单元格类.


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

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

[objc]   view plain  copy
  1. <span style="font-size:18px;">//进行返回每个section(节)中单元格的数量  
  2. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;  
  3.   
  4. // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:  
  5. // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)  
  6. // 为表视图中的单元格创建数据  
  7. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;</span>  
  除了以上的两个必须实现的方法,还有一些以下的可选实现方法:
[objc]   view plain  copy
  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:协议可以用来设定表视图中的节头与节尾 同时还可以去响应一些点击事件,主要的一些方法如下:
[objc]   view plain  copy
  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.设置代理方法。下面来看实例

      

[objc]   view plain  copy
  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.   
  9. #import "ZTTRootViewController.h"  
  10. #import "ZTTDetailsViewController.h"  
  11. #define  kDeviceHeight   [UIScreen mainScreen].bounds.size.height  
  12.   
  13. @interface ZTTRootViewController ()  
  14.   
  15. @end  
  16.   
  17. @implementation ZTTRootViewController  
  18.   
  19. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  20. {  
  21.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  22.     if (self) {  
  23.         self.title=@"UITableView Style";  
  24.     }  
  25.     return self;  
  26. }  
  27. -(void)loadView{  
  28.     UIView *view=[[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame];  
  29.     //[view setBackgroundColor:[UIColor redColor]];  
  30.     self.view=view;  
  31.     [view release];  
  32.       
  33.     //开始进行配置数据源  
  34.     self.listArray=@[@"UITableViewStylePlain",@"UITableViewStyleGrouped"];  
  35.     _tableView=[[UITableView alloc]initWithFrame:CGRectMake(00320,kDeviceHeight-20-44) style:UITableViewStylePlain];  
  36.     //实现数据源方法  
  37.     [_tableView setDataSource:self];  
  38.     //设置点击事件 代理方法  
  39.     [_tableView setDelegate:self];  
  40.     [self.view addSubview:_tableView];  
  41.       
  42. }  
  43. - (void)viewDidLoad  
  44. {  
  45.     [super viewDidLoad];  
  46.     // Do any additional setup after loading the view.  
  47. }  
  48.   
  49. - (void)didReceiveMemoryWarning  
  50. {  
  51.     [super didReceiveMemoryWarning];  
  52.     // Dispose of any resources that can be recreated.  
  53. }  
  54. #pragma mark- tableview date source  
  55. /* 
  56.  * 一个selection中又多少个单元格 
  57.  */  
  58. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  59. {  
  60.     return  [_listArray count];  
  61. }  
  62.   
  63. // indexPath  
  64. //创建单元格  
  65. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  66.     static NSString *cellInditifier=nil;  
  67.     // 创建单元格对象  
  68.     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellInditifier];  
  69.     if(cell ==nil){  
  70.         cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellInditifier]autorelease];  
  71.     }  
  72.     NSString *text=[self.listArray objectAtIndex:indexPath.row];  
  73.     cell.textLabel.text=text;  
  74.     return  cell;  
  75. }  
  76.   
  77.   
  78.   
  79. // 表视图中有几个selection  
  80. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{  
  81.     return  1;  
  82. }  
  83.   
  84.   
  85. // 选中单元格的方法  
  86. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
  87.     NSLog(@"didSelect");  
  88.     //进行跳转到相应的页面  
  89.     ZTTDetailsViewController *detailsVC=[[ZTTDetailsViewController alloc]init];  
  90.     detailsVC.isPlain=indexPath.row==0?YES:NO;  
  91.     [self.navigationController pushViewController:detailsVC animated:YES];  
  92.     [detailsVC release];  
  93. }  
  94. -(void)dealloc{  
  95.     [_tableView release];  
  96.     _tableView=nil;  
  97.     [super dealloc];  
  98. }  
  99. @end  
  100. </span>  
运行截图如下:


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

[objc]   view plain  copy
  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>  
     运行截图如下:

  


 


FROM: http://blog.csdn.net/developer_jiangqq/article/details/21627905


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐

  • 数据埋点:前端页面PV/UV的触发和交互

    数据埋点是数据分析的基础 依据埋点数据中我们可以开展数据清洗 数据归因 分析模型 AB测试等工作 如今数据分析可以说是当前最热门的技能了 不管是产品 运营还是设计都可以明显感知到各大平台 公众号都在使劲的推送 各种9 9秒杀课程 1元限时体
  • (转)玩4K必备知识:HDMI1.4、2.0、2.0a、2.0b接口参数对比

    HDMI接口又叫高清晰度多媒体接口 英文 High Definition Multimedia Interface HDMI 这是一种数字化视频 音频接口技术 是适合影像传输的专用型数字化接口 其可同时传送音频和影像信号 且无需在信号传送前
  • mybatis 动态sql 源码解析

    mybatis 动态sql 测试例子 java程序 xml文件 调试 直接进入到解析select语句的地方 进入 解析xml 进入buildStatementFromContext 依然是交给XMLStatementBuilder解析 核心
  • python飞机大战实战演练代码

    以下是主函数 import pygame from plane sprites import class PlaneGame object 飞机大战主游戏 def init self print 游戏初始化 1 创建游戏的窗口 self s
  • 澄清并发编程工具CountDownLatch的误区

    无论你对CountDownLatch的认知是通过看资料还是看博客 很多人都会存在一些误区 现在我也是站在巨人 某些大牛 的肩膀上去总结下这些误区 并把CountDownLatch的含义及用法仔细的演示一遍 1 常见误区 误区一 CountD
  • 【华为OD】

    目录 一 题目描述 二 输入描述 三 输出描述 用例 四 题目解析 五 Java玩法 六 JavaScript玩法 一 题目描述 某云短信厂商 为庆祝国庆 推出充值优惠活动 现在给出客户预算 和优惠售价序列 求最多可获得的短信总条数 二 输
  • 2021年江苏省职业院校技能大赛中职 网络信息安全赛项试卷--web安全渗透测试解析

    2021年江苏省职业院校技能大赛中职 网络信息安全赛项web安全渗透测试 2021年江苏省web安全渗透测试任务书 2021年江苏省web安全渗透测试任务书解析 如果有不懂得地方可以私信博主 欢迎交流 需要环境得 可以加博主联系方式 202
  • Ansible学习笔记2

    Ansible是Python开发的自动化运维工具 集合了众多运维工具 Puppet cfengine chef func fabric 的优点 实现了批量系统配置 批量程序部署 批量运行命令等功能 特点 1 部署简单 2 默认使用ssh进行
  • idea的代码文本距离左边很远问题解决

    idea的代码文本距离左边很远问题解决 刚才看了篇文章 是关于idea 2021 1版本 看起来挺好 然后忍不住尝试了一下 但是不知道做了什么操作 突然代码的左边距离行号非常远 然后找了很多文章 也没找到解决办法 重新安装idea仍旧没解决
  • springboot2.0集成ShardingSphere-jdbc5.0-alpha所遇到的一些坑

    在springboot 2 5 3中配置使用ShardingSphere 5 0 alpha遇到了不少的坑 现在总结如下 1 没有使用shardingsphere jdbc core spring boot starter 在使用Shard
  • centos下安装apache

    下载所需要的包 wget http mirrors tuna tsinghua edu cn apache httpd httpd 2 4 54 tar gz wget http mirrors tuna tsinghua edu cn a
  • GnuTLS recv error (-110): The TLS connection was non-properly terminated问题的解决方案

    我在使用git clone branch 3 4 depth 1 https github com opencv opencv git命令的时候 遇到如下问题 fatal unable to access https github com
  • Radware负载均衡项目配置实战解析之一初识RADWARE(VIP与FARM配置)

    在近期的项目中 接触负载均衡设备RADWARE比较多 可能有很多朋友对这个设备还不是很了解 所以我把具体项目实战中的Radware配置应用与大家做一个分享交流 RADWARE是一家智能应用网络解决方案的全球领先供应商 主要生产应用交付和网络
  • Ubuntu下无法打开终端

    在安装某个版本的python并进行一系列自己也看不懂的配置之后 突然发现终端无法打开了 具体来说就是从桌面点击没有用 ctrl alt T也打不开 只能从选择文件夹 右击选择终端打开时可以 然后开始救赎之路 打开vscode的终端 或pyc
  • Python开发难学吗?简单易用适合初学者入门

    Python开发难学吗 适合初学者吗 Python入门阶段零基础学员打好基础是非常重要的 在非常高的抽象计算中 高级的Python程序设计非常难学 高级程序语言不等于简单 但对于初学者和完成普通任务Python语言是非常简单易用的 Pyth
  • 四、【服务器】服务器入门——常见单位

    U 高度单位 U 是UNIT的缩写 1U 4 445cm 是一种表示机架服务器外部尺寸的单位 指的是高度或者说厚度 规定这个尺寸是为了使得服务器保持适当的尺寸 在机柜安装的时候需要注意到留好安装高度 机架服务器通常宽度在19英寸 高度为1U
  • 小波分析

    本文首先介绍了从傅里叶变换到小波变换的发展史 然后着重强调了小波变换的两种作用 时频分析和多分辨率分析 最后讲了一下吉布斯效应等相关知识 FT 傅里叶变换 通过将信号分解成正余弦函数 把三角函数当做函数空间的基 将时域信号转化为频域信号 缺
  • Ubuntu16.04升级docker ce错误:Depends: libseccomp2 (>= 2.3.0) but 2.2.3-3ubuntu3 is to be installed

    环境 系统 Ubuntu16 04 docker old version Docker version 1 10 3 build 20f81dd 按照官方给出的步骤安装 https docs docker com install linux
  • DGL-kernel的变更(3)

    导读 DGL kernel中针对Graph的计算几个版本有了不小的变动 v0 3 0 4使用的是minigun v0 3和v0 4源码中主要相关部分则是在对应分支dgl src kernel目录下 v0 5中对kernel代码进行了重写 并
  • IOS学习笔记(十二)之IOS开发之表视图(UITableView)的相关类,属性与表视图实现学习(二)

    iOS学习笔记 十二 之IOS开发之表视图 UITableView 的讲解与使用 二 博客地址 http blog csdn net developer jiangqq 转载请注明地址 Author hmjiangqq Email jian