NSIndexPath 延伸
转载自:http://my.oschina.net/u/2560887/blog/602095?fromerr=Dy4vj5Jd
这个类的实例描述了一个嵌套数组中特定节点的路径,一般叫做索引路径.
1.4.3.2
索引路径中的每一个索引值描述了在那一层数组中的位置.
A1
0 A2
1 ---- 0
2 | 1
3 | 2
4 | 3
5 |-------->4
第一次接触这个类是因为tableview的行标记,后来发现这个不全面,专门看了发现对这个类理解有偏差:
SDK里是这么定义这个类的:The NSIndexPath class represents the path to a specific node in a tree of nested array collections. This path is known as an index path. 也就是说这个类其实是表示的结点的索引路径。官方文档有图,不明白的看一下官方文档这个类介绍的开始那个图,一目了然这个类是干嘛的。其实它可以表示的不光是俩个数组的情况,像我们熟知的tableview的行。tableview的行其实是它的最简单的一种了。
一. 创建索引路径 create indexPath object
1.1 创建一个结点的索引路径 create an one-node index path
|
1
2
|
NSIndexPath *oneNodeIndexPath = [NSIndexPath indexPathWithIndex:6];NSLog(@"oneNodeIndexPathvv:%@", oneNodeIndexPath); |
控制台输出:
oneNodeIndexPathvv:<NSIndexPath: 0xc00000000000060e> {length = 1, path = 6}
1.2 创建一个或者多个结点的索引路径 create an index path with one or more nodes
length>indexs.count时会出现什么情况,后面的路径不准确.length<indexs.count会截取indexs的前length个数字组成node的path。总之length<indexs.count的话路径准确。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
// one nodeNSUInteger indexs[] = {1};//定义并初始化一个C数组:1个元素NSIndexPath *oneNodeIndexPath = [NSIndexPath indexPathWithIndexes:indexs length:1];NSLog(@"oneNodeIndexPath:%@", oneNodeIndexPath);// two nodesNSUInteger indexs2[] = {1, 2};//定义并初始化一个C数组:2个元素NSIndexPath *twoNodeIndexPath = [NSIndexPath indexPathWithIndexes:indexs2 length:2];NSLog(@"twoNodeIndexPath:%@", twoNodeIndexPath);// three nodesNSUInteger indexs3[] = {1, 2 , 3 };//定义并初始化一个C数组:3个元素NSIndexPath *threeNodeIndexPath = [NSIndexPath indexPathWithIndexes:indexs3 length:3];NSLog(@"threeNodeIndexPath:%@", threeNodeIndexPath);// four nodesNSUInteger indexs4[] = {4, 2, 3 , 4};//定义并初始化一个C数组:4个元素NSIndexPath *fourNodeIndexPath = [NSIndexPath indexPathWithIndexes:indexs4 length:4];NSLog(@"fourNodeIndexPath:%@", fourNodeIndexPath); |
控制台输出:
oneNodeIndexPath:<NSIndexPath: 0xc00000000000010e> {length = 1, path = 1}
twoNodeIndexPath:<NSIndexPath: 0xc000000000400116> {length = 2, path = 1 - 2}
threeNodeIndexPath:<NSIndexPath: 0xc000000c0040011e> {length = 3, path = 1 - 2 - 3}
fourNodeIndexPath:<NSIndexPath: 0xc002000c00400426> {length = 4, path = 4 - 2 - 3 - 4}
1.3 在tableview中的代表行索引的NSIndexPath对象创建:
NSIndexPath这个类本身在Foundation框架下,而这个方法是在UIKit下的。UIKIt里给NSIndexPath写了个category 针对于UITableView。 indexPath with two nodes
|
1
2
|
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:9];NSLog(@"indexPath: %@", indexPath); |
控制台输出:
indexPath: <NSIndexPath: 0xc000000000000916> {length = 2, path = 9 - 0}
1.4 在collection中的代表索引的NSIndexPath对象的创建:下面这个同样在UIKIt框架下,针对UICollectionViewAdditions。 indexPath with two nodes
|
1
2
|
NSIndexPath *indexPathForCollection = [NSIndexPath indexPathForItem:1 inSection:3];NSLog(@"indexPathForCollection : %@", indexPathForCollection); |
控制台输出:
indexPathForCollection : <NSIndexPath: 0xc000000000200316> {length = 2, path = 3 - 1}
上面的都是类方法初始化出一个NSIndexPath对象,也可以用init对象方法初始化出一个NSIndexPath对象。
二. 询问索引路径 Querying Index Paths
2.1 provide the index at particular node in the index path 提供特定(指定)节点的索引值 也就是返回第几个节点的索引值
|
1
2
3
4
5
6
|
NSUInteger index0 = [fourNodeIndexPath indexAtPosition:0];//这个参数对应上面创建方法二中的indexs中的数组的元素下标。这个方法是要取出对应下标下的值。outside the range of the index path如果超过了indexs.count-1,则返回值不确定。NSLog(@"index0 : %lu", (unsigned long)index0);NSLog(@"index2 : %lu", (unsigned long)[fourNodeIndexPath indexAtPosition:1]);NSLog(@"index2 : %lu", (unsigned long)[fourNodeIndexPath indexAtPosition:2]);NSLog(@"index3 : %lu", (unsigned long)[fourNodeIndexPath indexAtPosition:3]);NSLog(@"index4 : %lu", (unsigned long)[fourNodeIndexPath indexAtPosition:4]); |
控制台输出:
index0 : 4
index2 : 2
index2 : 3
index3 : 4
index4 : 9223372036854775807
可以看出传的参数position>indexes.count的话,即超过了会返回不确定值得。
2.2 给原有index path 增加一个node生成一个新的index path
|
1
2
3
4
|
NSIndexPath *newAddIndex10 = [fourNodeIndexPath indexPathByAddingIndex:10];NSLog(@"\nfourNodeIndexPath: %@,newAddIndex10:%@", fourNodeIndexPath,newAddIndex10 );NSIndexPath *newAddIndex2 = [oneNodeIndexPath indexPathByAddingIndex:2];NSLog(@"\noneNodeIndexPath: %@,newAddIndex2:%@", oneNodeIndexPath,newAddIndex2 ); |
控制台输出:
fourNodeIndexPath: <NSIndexPath: 0xc002000c00400426> {length = 4, path = 4 - 2 - 3 - 4},newAddIndex10:<NSIndexPath: 0x7fa3f2d0c060> {length = 5, path = 4 - 2 - 3 - 4 - 10}
oneNodeIndexPath: <NSIndexPath: 0xc00000000000010e> {length = 1, path = 1},newAddIndex2:<NSIndexPath: 0xc000000000400116> {length = 2, path = 1 - 2}
2.3 给原有index path 删除最后一个node的index(remove last index ),生成一个新的index path
|
1
2
3
4
|
NSIndexPath *removingLastIndexFourNode = [fourNodeIndexPath indexPathByRemovingLastIndex];NSLog(@"\nfourNodeIndexPath: %@,removingLastIndexFourNode:%@", fourNodeIndexPath,removingLastIndexFourNode );NSIndexPath *removingLastIndexOneNode = [oneNodeIndexPath indexPathByRemovingLastIndex];NSLog(@"\noneNodeIndexPath: %@,removingLastIndexOneNode:%@", oneNodeIndexPath,removingLastIndexOneNode ); |
控制台输出:
fourNodeIndexPath: <NSIndexPath: 0xc002000c00400426> {length = 4, path = 4 - 2 - 3 - 4},removingLastIndexFourNode:<NSIndexPath: 0xc000000c0040041e> {length = 3, path = 4 - 2 - 3}
oneNodeIndexPath: <NSIndexPath: 0xc00000000000010e> {length = 1, path = 1},removingLastIndexOneNode:<NSIndexPath: 0xc000000000000006> {length = 0, path = }
2.4 length :(索引路径的索引数组元素的个数)the number of indexs in the index path 这个属性其实在NSLog方法输出索引对象时就会显示的。
|
1
2
|
NSUInteger le = [fourNodeIndexPath length];NSLog(@"le :%lu", (unsigned long)le); |
控制台输出:
le :4
2.5
getIndexes:range:这个方法 不理解
拷贝存储在索引路径中的索引数组(indexes)从由position range指定的indexes到特定的indexes(specified indexes)。我这么理解的,但使用出错了,正在探索纠正中...
三. comparing Index Path
说到这个就说一下NSString的比较。凡是比较,在OC大多返回的是NSComparisonResult,它是枚举值,三个:NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending,分别代表升序,相等,降序。
|
1
2
3
4
5
6
7
8
9
10
|
NSComparisonResult result = [oneNodeIndexPath compare:twoNodeIndexPath];NSLog(@"result : %ld", (long) result);NSComparisonResult result1 = [threeNodeIndexPath compare:twoNodeIndexPath];NSLog(@"result1 : %ld", (long) result1);NSComparisonResult result2 = [threeNodeIndexPath compare:threeNodeIndexPath];NSLog(@"result2 : %ld", (long) result2); |
控制台输出:
result : -1
result1 : 1
result2 : 0
转载自:http://blog.csdn.net/houseq/article/details/37690049
========================NSIndexPath========================
--简介:The NSIndexPath class represents the path to a specific node in a tree of nested array collections. This path is known as an index path。NSIndexPath类代表着“嵌套数组的树”中得某节点的路径。如下图:
NSIndexPath 让你精确指定一个树结构 data structure 里面的某个节点的数据。比如你有一个 NSArray, 里面很多节点,每个节点又是一个 NSArray,每个节点的这个里面又是一个NSArray,然后下面又是一个 NSArray,这样简单说起来,你有一个四层的 NSarray ,每层下面都有好多个 NSArray。然后你造一个NSIndexPath 1.3.4.2。你就可以拿它访问,第一层的第三个节点之下的第四个节点的第二个节点的数据。
------------常用方法和属性------------
--用Index 或者 index数组和路径的长度 创建NSIndexPath;
- + (instancetype)indexPathWithIndex:(NSUInteger)index;
- + (instancetype)indexPathWithIndexes:(const NSUInteger [])indexes length:(NSUInteger)length;
- - (instancetype)init; /* designated initializer */
- - (instancetype)initWithIndexes:(const NSUInteger [])indexes length:(NSUInteger)length; /* designated initializer */
--修改:注意返回的是一个新的对象,不是原来的对象基础上改得。
- - (NSIndexPath *)indexPathByAddingIndex:(NSUInteger)index;
- - (NSIndexPath *)indexPathByRemovingLastIndex;
--访问属性、比较
- - (NSUInteger)indexAtPosition:(NSUInteger)position;
- - (NSUInteger)length; //路径包含节点个数 或 “路径长度"
- // comparison support
- - (NSComparisonResult)compare:(NSIndexPath *)otherObject;
--例:
- NSIndexPath *indexPath = [NSIndexPath indexPathWithIndex:1];
- NSLog(@"%@ length:%D ",indexPath,indexPath.length); //<NSIndexPath: 0x8a0ea20> {length = 1, path = 1} length:1
- indexPath = [indexPath indexPathByAddingIndex:24];
- NSLog(@"%@ length:%D ",indexPath,indexPath.length); //<NSIndexPath: 0x8c54090> {length = 2, path = 1 - 24} length:2
- NSLog(@"postion:%d",[indexPath indexAtPosition:1]); //postion:24
备注:
1.在UITableview.h中定义着NSIndexPath的分类。用它来表示tableview中某section中某行,为了方便加了一些属性和方法:
- // This category provides convenience methods to make it easier to use an NSIndexPath to represent a section and row
- @interface NSIndexPath (UITableView)
- + (NSIndexPath *)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;
- @property(nonatomic,readonly) NSInteger section;
- @property(nonatomic,readonly) NSInteger row;
NSIndexPath 延伸的更多相关文章
- 采用MiniProfiler监控EF与.NET MVC项目(Entity Framework 延伸系列1)
前言 Entity Framework 延伸系列目录 今天来说说EF与MVC项目的性能检测和监控 首先,先介绍一下今天我们使用的工具吧. MiniProfiler~ 这个东西的介绍如下: MVC Mi ...
- 采用EntityFramework.Extended 对EF进行扩展(Entity Framework 延伸系列2)
前言 Entity Framework 延伸系列目录 今天我们来讲讲EntityFramework.Extended 首先科普一下这个EntityFramework.Extended是什么,如下: 这 ...
- 《高性能javascript》一书要点和延伸(下)
第六章 快速响应的用户界面 本章开篇介绍了浏览器UI线程的概念,我也突然想到一个小例子,这是写css3动画的朋友都经常会碰到的一个问题: <head> <meta charset=& ...
- 【Python五篇慢慢弹(5)】类的继承案例解析,python相关知识延伸
类的继承案例解析,python相关知识延伸 作者:白宁超 2016年10月10日22:36:57 摘要:继<快速上手学python>一文之后,笔者又将python官方文档认真学习下.官方给 ...
- 【NLP】条件随机场知识扩展延伸(五)
条件随机场知识扩展延伸 作者:白宁超 2016年8月3日19:47:55 [摘要]:条件随机场用于序列标注,数据分割等自然语言处理中,表现出很好的效果.在中文分词.中文人名识别和歧义消解等任务中都有应 ...
- iOS - 分析JSON、XML的区别和解析方式的底层是如何实现的(延伸实现原理)
<分析JSON.XML的区别,JSON.XML解析方式的底层是如何实现的(延伸实现原理)> (一)JSON与XML的区别: (1)可读性方面:基本相同,XML的可读性比较好: (2)可扩展 ...
- 转型?还是延伸?开源建站系统近乎推整套SNS社区解决方案
转型?还是延伸?开源建站系统近乎推整套SNS社区解决方案 近乎(英文:Spacebuilder),作为.net领域的SNS社区建站系统代表之一,一直在技术开发领域算是兢兢业业,在Discuz!和Php ...
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath不执行的问题
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPa ...
- json学习系列(6)JSONObject和JSONArray是JDK的集合部分延伸
我一直觉得JSONObject和JSONArray是JDK集合部分的延伸,它们与JDK的List和Map一脉相承.通过研究JSONObject和JSONArray的结构,我们顺便也复习一下JDK的内容 ...
随机推荐
- Business Intelligence(BI) 商务智能
商业智能技术正是一种能够帮助企业迅速地完成信息采集.分析的先进技术.它以数据仓库(Data Warehousing).在线分析处理(OLAP).数据挖掘(Data Mining)3种技术的整合为基础, ...
- aspose 解决插入html后字体问题
/// <summary> /// 添加html /// </summary> /// <param name="strText"></p ...
- KeyTweak(笔记本键盘设置工具) V2.20 中文版
软件名称: KeyTweak(笔记本键盘设置工具)软件语言: 简体中文授权方式: 免费软件运行环境: Win 32位/64位软件大小: 50KB图片预览: 软件简介:KeyTweak 通过设置系统的注 ...
- Crowd安装和破解
参考资料来自:http://www.it165.net/os/html/201311/6653.html 这个破解比较简单
- Windows下QT4.8.4编译环境的搭建(转载http://blog.csdn.net/bestgonghuibin/article/details/38933141)
开始使用QT了,所以第一步就是把环境搭起来,这里小记一下,以免以后忘记. 1. 下载安装文件 要使用QT功能,那么必须要下载QT的源码,还必须要一个是用QT的编译环境,可以是VS2010,也可以是专用 ...
- 【原创】mdk5宏定义的使用小结
前几天在网上申请了一块芯片为stm32f103cbt6的小板子. 在用keil编程的过程中发现一些小问题,总结如下: 使用mdk5开发,与之前的库函数的引用方式已经改变.不需要在选项的c/c++标签页 ...
- HDU 5352 MZL's City
最小费用最大流,因为要控制字典序,网络流控制不好了...一直WA,所以用了费用流,时间早的费用大,时间晚的费用少. 构图: 建立一个超级源点和超级汇点.超级源点连向1操作,容量为K,费用为COST,然 ...
- SpringMVC 学习-入门篇
一.都需要哪些 Jar 包 <dependencies> <dependency> <groupId>org.springframework</groupId ...
- 无法识别的配置节 system.serviceModel
也是从网上四处搜来的答案,的确是解决了问题 不知道是不是补丁更新的原因,之前运行好的程序,突然就不行了, 一开始,运行直接就闪退了,在事件查看器里,也看不到具体的错误信息,幸亏是cmd的程序,所以 , ...
- HDU1236:排名
Problem Description 今天的上机考试虽然有实时的Ranklist,但上面的排名只是根据完成的题数排序,没有考虑 每题的分值,所以并不是最后的排名.给定录取分数线,请你写程序找出最后 ...