UITableView知识梳理须知—(一)

1、UITableView掌握

      1>  设置UITableView的dataSource、delegate 

      2>    UITableView多组数据和单组数据的展示 

      3>  UITableViewCell的常见属性 

      4>    UITableView的性能优化(cell的循环利用)

      5>  自定义Cell

2、什么是UITableView

    在iOS中,要实现展示列表数据,最常用的做法就是使用UITableView。UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳

3、如何展示数据  

  1.  UITableView需要一个数据源(dataSource)来显示数据

  2. UITableView会向数据源查询一共有多少行数据以及每一行显示什么数据等

  3. 没有设置数据源的UITableView只是个空壳

  4. 凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的数据源

4、UITableViewCell

4.1  UITableViewCell简介:

 UITableView的每一行都是一个UITableViewCell,通过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每一行

 UITableViewCell内部有个默认的子视图:contentView,contentView是UITableViewCell所显示内容的父视图,可显示一些辅助指示视图

辅助指示视图的作用是显示一个表示动作的图标,可以通过设置UITableViewCell的accessoryType来显示,默认是UITableViewCellAccessoryNone(不显示辅助指示视图),其他值如下:
UITableViewCellAccessoryDisclosureIndicator
UITableViewCellAccessoryDetailButton
UITableViewCellAccessoryDetailDisclosureButton
UITableViewCellAccessoryCheckmark
还可以通过cell的accessoryView属性来自定义辅助指示视图(比如往右边放一个开关),cell的属性accessoryView的优先级高于属性accessoryType

4.2 UITableViewCell的contentView

   contentView下默认有3个子视图

1、其中2个是UILabel(通过UITableViewCell的textLabeldetailTextLabel属性访问)
2、第3个是UIImageView(通过UITableViewCell的imageView属性访问)
3、UITableViewCell还有一个UITableViewCellStyle属性,用于决定使用contentView的哪些子视图,以及这些子视图在contentView中的位置     

4.3 UITableViewCell结构

4.4 UITableViewCell的重用原理

iOS设备的内存有限,如果用UITableView显示成千上万条数据,就需要成千上万个UITableViewCell对象的话,那将会耗尽iOS设备的内存。要解决该问题,需要重用UITableViewCell对象     
     Cell重用原理:当滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待重用。当UITableView要求dataSource返回UITableViewCell时,dataSource会先查看这个对象池,如果池中有未使用的UITableViewCell,dataSource会用新的数据配置这个UITableViewCell,然后返回给UITableView,重新显示到窗口中,从而避免创建新对象
    
还有一个非常重要的问题:有时候需要自定义UITableViewCell(用一个子类继承UITableViewCell),而且每一行用的不一定是同一种UITableViewCell,所以一个UITableView可能拥有不同类型的UITableViewCell,对象池中也会有很多不同类型的UITableViewCell,那么UITableView在重用UITableViewCell时可能会得到错误类型的UITableViewCell
解决方案:UITableViewCell有个NSString *reuseIdentifier属性,可以在初始化UITableViewCell的时候传入一个特定的字符串标识来设置reuseIdentifier(一般用UITableViewCell的类名)。当UITableView要求dataSource返回UITableViewCell时,先通过一个字符串标识到对象池中查找对应类型的UITableViewCell对象,如果有,就重用,如果没有,就传入这个字符串标识来初始化一个UITableViewCell对象

4.5 Cell的重用代码

 1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 2 {
 3     // 1.定义一个cell的标识
 4       static NSString *ID = @”czcell";
 5     
 6     // 2.从缓存池中取出cell
 7       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
 8     
 9     // 3.如果缓存池中没有cell
10       if (cell == nil) {
11         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
12     }

5、UITableView、UITableViewController、代理与数据源之间的关系,如下图所属:

6、UITableView和数据源

 

1. tableView展示数据

    // 1.调用数据源的下面方法得知一共有多少组数据

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

  // 2.调用数据源的下面方法得知每一组有多少行数据

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

   // 3.调用数据源的下面方法得知每一行显示什么内容

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

7、UITableView的常见属性

   1. 常用属性

    //1. 修改tableView的行高

self.tableView.rowHeight = 100;

 // 2.组头组尾的高

 self.tableView.sectionHeaderHeight = 55;self.tableView.sectionFooterHeight = 22;

 // 3.设置整个tablView的头部/尾部视图

  self.tableView.tableHeaderView = [[UISwitch alloc] init];self.tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeInfoDark];

 // 4.设置我们分割线颜色(clearColor相当于取消系统分割线)

self.tableView.separatorColor = [UIColor clearColor];

 // 5.设置分割线样式

 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

// 设置索引条内部文字颜色

self.tableView.sectionIndexColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];

// 设置索引条背景颜色

self.tableView.sectionIndexBackgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];

// 允许UITableView多选

self.tableView.allowsMultipleSelection = YES; 

 

 

// 获取选中多行

NSArray *array = self.tableView.indexPathsForSelectedRows; 

 // 获取选中单行

NSIndexPath *indexPath = self.tableView.indexPathForSelectedRow;

 // 让UITableView进入编辑状态,会出现左滑效果(结合代理方法commitEditingStyle...处理左滑效果时编辑事件处理)

self.tableView.editing = YES;

   2、常见方法(数据源与代理方法)

#pragma mark - 数据源方法
// 返回行数
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
}
// 设置cell - (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{ 。。。。。。。。。。。 }#pragma mark - 代理方法 /** * 设置行高 */ - (CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{return 100; }// 添加每组的组头 - (UIView *)tableView:(nonnull UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
}// 返回每组的组尾 - (UIView *)tableView:(nonnull UITableView *)tableView viewForFooterInSection:(NSInteger)section{}// 选中某行cell时会调用 - (void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{NSLog(@"选中didSelectRowAtIndexPath row = %ld", indexPath.row); }/* 2015-07-21 10:01:56.261 02-UITableView单组数据展示[1305:36752] 选中didSelectRowAtIndexPath row = 0 2015-07-21 10:01:58.212 02-UITableView单组数据展示[1305:36752] 取消选中 didDeselectRowAtIndexPath row = 0 2015-07-21 10:01:58.212 02-UITableView单组数据展示[1305:36752] 选中didSelectRowAtIndexPath row = 1 */ // 取消选中某行cell会调用 (当我选中第0行的时候,如果现在要改为选中第1行 - 》会先取消选中第0行,然后调用选中第1行的操作) - (void)tableView:(nonnull UITableView *)tableView didDeselectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{NSLog(@"取消选中 didDeselectRowAtIndexPath row = %ld ", indexPath.row); }// 设置UITableView的索引条方法: // 设置UITableView的索引条,返回数组字符串集合 - (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView;{NSArray *carGroupModels = self.carGroups;// 利用KVC获取指定属性的集合NSArray *array = [carGroupModels valueForKeyPath:@"title"];return array; }
索引条颜色与背景设置:
 // 设置索引条内部文字颜色self.tableView.sectionIndexColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];// 设置索引条背景颜色self.tableView.sectionIndexBackgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];

3、数据刷新

注意:
  * 添加刷新:使用UITableView的 insertRowsAtIndexPaths...方法
  * 修改刷新:使用UITableView的 reloadRowsAtIndexPaths...方法(该方法使用的前提是模型数据的个数不变,所以添加与删除不能采用此方法进行UITableView刷新功能。)
  * 删除刷新:使用UITableView的 deleteRowsAtIndexPaths...方法

谢谢自己的坚持 ,而没有懈怠,今天就到处结束,明天我讲系统的梳理自定义等高与不等高UITableViewCell,分别用frame、xib、storyboard、Autolayout实现,也将在其中使用第三方框架:Masonry与MJExtension,以便大家互相学习。睡觉了,晚安,^_^

转载于:https://www.cnblogs.com/cjpBlog/p/4669126.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/375709.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Yarn中的几种状态机

1 概述 为了增大并发性&#xff0c;Yarn采用事件驱动的并发模型&#xff0c;将各种处理逻辑抽象成事件和调度器&#xff0c;将事件的处理过程用状态机表示。什么是状态机&#xff1f; 如果一个对象&#xff0c;其构成为若干个状态&#xff0c;以及触发这些状态发生相互转移的事…

反转字符串里的单词

4、反转字符串里的单词 给定一个字符串&#xff0c;逐个反转字符串中的单词 示例1&#xff1a; 输入: "the sky is blue", 输出: "blue is sky the".说明&#xff1a; 无空格字符构成一个单词。 输入字符串可以在前面或者后面包含多余的空格&#xff0…

正整数

题目链接&#xff1a;http://acm.hust.edu.cn/vjudge/contest/view.action?cid84077#problem/A 题目&#xff1a; Description A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the s…

360 webscan中防注入跨站攻击的核心

//get拦截规则 $getfilter "\\<.javascript:window\\[.{1}\\\\x|<.*(&#\\d?;?)?>|<.*(data|src)data:text\\/html.*>|\\b(alert\\(|confirm\\(|expression\\(|prompt\\(|benchmark\s*?\\(\d?|sleep\s*?\\([\d\.]?\\)|load_file\s*?\\()|<[…

POJ 2115 C Looooops(扩展欧几里得)

辗转相除法&#xff08;欧几里得算法&#xff09; 时间复杂度&#xff1a;在O(logmax(a, b))以内 int gcd(int a, int b) {if (b 0) return a;return gcd(b, a % b); }扩展欧几里得算法 时间复杂度和欧几里得算法相同 int extgcd(int a, int b, int& x, int& y) {int …

分支管理(转载)

转自&#xff1a;http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013743862006503a1c5bf5a783434581661a3cc2084efa000 分支就是科幻电影里面的平行宇宙&#xff0c;当你正在电脑前努力学习Git的时候&#xff0c;另一个你正在另一个平行…

匹配括号

输入&#xff1a; 仅包含{,},(,),[,]的字符串输出&#xff1a; 如果括号匹配输出&#xff1a;YES 否则输出&#xff1a;NOSolution&#xff1a; #include<iostream> #include<string> #include<stack> using namespace std;bool check(const string&)…

总线接口与计算机通信

微机中总线一般有内部总线、系统总线和外部总线。 内部总线是微机内部各外围芯片与处理器之间的总线&#xff0c;用于芯片一级的互连&#xff1b; 系统总线是微机中各插件板与系统板之间的总线&#xff0c;用于插件板一级的互连&#xff1b; 外部总线则是微机和外部设备之间的总…

uva 12442 . Forwarding Emails

“... so forward this to ten other people, to prove that you believe the emperor has new clothes.”Aren’t those sorts of emails annoying?Martians get those sorts of emails too, but they have an innovative way of dealing with them.Instead of just forwardi…

大数相加

输入&#xff1a; 两个用字符串表示的大整数 如a1111111111111,b222222222222222 输出&#xff1a; 两个数的和 Solution&#xff1a; #include<iostream> #include<algorithm> #include<string>using namespace std;int add(const char&,const char&…

Linux的进程与服务(一)

启动的配置文件/etc/inittab&#xff0c;修改完配置文件以后 init q立即生效 # Default runlevel. The runlevels used by RHS are: # 0 - halt (Do NOT set initdefault to this) # 1 - Single user mode # 2 - Multiuser, without NFS (The same as 3, if you do not h…

Linux 修改swap虚拟内存大小

swap是内存的交换区&#xff1b;换句话说&#xff0c;如果内存不够用了&#xff0c;那么系统会在硬盘上存储一些内存中不常用的数据&#xff0c;之后将这部分数据在存储中析构掉&#xff1b;这样内存就又有剩余空间可以运行东东啦&#xff0c;这个过程也就是所谓的交换&#xf…

统计文章中的单词

输入&#xff1a; 字符串&#xff0c;其中可能包含空格&#xff0c;TAB&#xff0c;回车等&#xff0c;规定&#xff0c;仅字母数字和单引号算作单词部分 输出&#xff1a; 单词的个数 Solution&#xff1a; #include<iostream> #include<string>using namespac…

迈向世界 拓展未来

一切都会过去&#xff0c;只有真理永存&#xff0c;只有愿意越过事实前进一步的人&#xff0c;才能理解事实&#xff0c;这就是科学。时代在发展&#xff0c;科技更是日新月异彻底改变着我们的生活方式。现在的我们就是跟着科技发展的脚步&#xff0c;奔着梦想&#xff0c;一直…

JS - 跳转页面

<!-- 第一种&#xff1a; --><script type"text/javascript">window.location.href "login.jsp?backurl" window.location.href;</script><!-- 第二种&#xff1a; --><script type"text/javascript"&g…

分享一个用安卓手机就能引导pc安装linux系统办法

1、首先安卓手机下载软件DriveDroid.apk http://pan.baidu.com/s/1qW4pbT6 2、下载linux镜像文件放手机存储卡存储&#xff0c;放到Download/images/以下 3、打开软件会自己主动读取这个目录以下镜像&#xff0c;也能够在软件里面下载须要的镜像文件 4、软件设置usb连接模式 5、…

SharePoint 2013 开发——其他社交功能

博客地址&#xff1a;http://blog.csdn.net/FoxDave上一篇讲了如何获取用户配置文件的相关属性&#xff0c;它属于SharePoint 2013社交功能的一个小的构成部分。社交功能是SharePoint 2013改进的一大亮点。可以在现有网站上开启社交功能或者新建一个专门用于社交用途的社区网站…

第一个Qt+opencv程序

简单安装好Qt和编译安装好opencv后&#xff0c;简单实现第一个Qtopencv程序&#xff1a;读取并显示一张图片&#xff0c;这里我的Qt版本时5.9.1&#xff0c;opencv版本是4.0.1&#xff0c;版本的影响不大。 首先我们用Qt创建一个控制台项目&#xff0c;即在创建项目时选择Qt C…

redis学习笔记——应用场景

最近在看redis入门指南&#xff0c;现在就自己的学习情况说说自己的理解。 字符串类型&#xff08;String&#xff09; 字符串类型是Redis中最基本的类型&#xff0c;能存储任意形式的字符串&#xff0c;包括二进制数据。如一张照片也可以用字符串类型存储。注意字符串类型键允…

Unity的Cover flow的实现包(2个)

苹果的mac机上预览图片&#xff0c;有一个所谓的cover flow的效果&#xff0c;这里收集到两个&#xff0c;两个实现效果略有不同。 1、老外的实现 https://github.com/rakkarage/Unity3D-CoverFlow 这个焦点图片在到最后位置前会模拟一个抖动效果 2、国人的实现 http://game.ce…