第一次接触这个类是由于tableview的行标记,后来发现这个不全面,专门看了发现对这个类理解有误差: node
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的行实际上是它的最简单的一种了。 web
一. 建立索引路径 create indexPath object 数组
1.1 建立一个结点的索引路径 create an one-node index path 框架
NSIndexPath *oneNodeIndexPath = [NSIndexPath indexPathWithIndex:6]; NSLog(@"oneNodeIndexPathvv:%@", oneNodeIndexPath);
控制台输出: ide
oneNodeIndexPathvv:<NSIndexPath: 0xc00000000000060e> {length = 1, path = 6} spa
1.2 建立一个或者多个结点的索引路径 create an index path with one or more nodes code
length>indexs.count时会出现什么状况,后面的路径不许确.length<indexs.count会截取indexs的前length个数字组成node的path。总之length<indexs.count的话路径准确。 对象
// one node NSUInteger indexs[] = {1};//定义并初始化一个C数组:1个元素 NSIndexPath *oneNodeIndexPath = [NSIndexPath indexPathWithIndexes:indexs length:1]; NSLog(@"oneNodeIndexPath:%@", oneNodeIndexPath); // two nodes NSUInteger indexs2[] = {1, 2};//定义并初始化一个C数组:2个元素 NSIndexPath *twoNodeIndexPath = [NSIndexPath indexPathWithIndexes:indexs2 length:2]; NSLog(@"twoNodeIndexPath:%@", twoNodeIndexPath); // three nodes NSUInteger indexs3[] = {1, 2 , 3 };//定义并初始化一个C数组:3个元素 NSIndexPath *threeNodeIndexPath = [NSIndexPath indexPathWithIndexes:indexs3 length:3]; NSLog(@"threeNodeIndexPath:%@", threeNodeIndexPath); // four nodes NSUInteger indexs4[] = {4, 2, 3 , 4};//定义并初始化一个C数组:4个元素 NSIndexPath *fourNodeIndexPath = [NSIndexPath indexPathWithIndexes:indexs4 length:4]; NSLog(@"fourNodeIndexPath:%@", fourNodeIndexPath);
控制台输出: 索引
oneNodeIndexPath:<NSIndexPath: 0xc00000000000010e> {length = 1, path = 1} three
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
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
NSIndexPath *indexPathForCollection = [NSIndexPath indexPathForItem:1 inSection:3]; NSLog(@"indexPathForCollection : %@", indexPathForCollection);
控制台输出:
indexPathForCollection : <NSIndexPath: 0xc000000000200316> {length = 2, path = 3 - 1}
上面的都是类方法初始化出一个NSIndexPath对象,也能够用init对象方法初始化出一个NSIndexPath对象。
二. 询问索引路径 Querying Index Paths2.1 provide the index at particular node in the index path 提供特定(指定)节点的索引值 也就是返回第几个节点的索引值
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
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
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方法输出索引对象时就会显示的。
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,分别表明升序,相等,降序。
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
四.额外的
4.1 NSString 的比较
介绍3个比较方法:1.compare:2.compare:option:3.compare:option:range。这三个compare方法都是对象方法,返回值是NSComparisonResult,仍是升序,相同,降序。
参数说明:compare后面是要与调用该方法的字符串比较的字符串。option是比较时注意的,是NSStringCompareOptions对象,这是枚举值,对其各个值以下说明:
// 枚举用于搜索和比较 NSStringCompareOptions option1 = NSCaseInsensitiveSearch;//不区分大小写比较 NSStringCompareOptions option2 = NSLiteralSearch;//区分大小写比较 NSStringCompareOptions option4 = NSBackwardsSearch;//从字符串末尾开始搜索 NSStringCompareOptions option8 = NSAnchoredSearch;//搜索限制范围的字符串 NSStringCompareOptions option64 = NSNumericSearch;//按照字符串里的数字为依据,算出顺序。 NSStringCompareOptions option128 = NSDiacriticInsensitiveSearch;//忽略“—”符号的比较 NSStringCompareOptions option256 = NSWidthInsensitiveSearch;//忽略字符串的长度,比较出结果。 NSStringCompareOptions option512 = NSForcedOrderingSearch;//忽略不区分大小写比较的选项,并强制返回NSOrderedAscending 或者NSOrderedDescending NSStringCompareOptions option1024 = NSRegularExpressionSearch;//只能应用于 rangeOfString:..., stringByReplacingOccurrencesOfString:...和 replaceOccurrencesOfString:... 方法。使用通用兼容的比较方法,若是设置此项,能够去掉 NSCaseInsensitiveSearch 和 NSAnchoredSearch
range是NSRange对象,对其说明以下:
range:(NSRange) 比较字符串的范围 结构变量: location:须要比较的字符串起始位置:(以0为开始) length:须要比较的字符串长度