iOS - NSIndexPath

第一次接觸這個類是由於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 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:須要比較的字符串長度
相關文章
相關標籤/搜索