iOS - NSIndexPath

第一次接触这个类是因为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

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的话路径准确。

// 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}

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 Paths

2.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:需要比较的字符串长度


转载于:https://my.oschina.net/u/2560887/blog/602095

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值