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

 

?
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;

[objc]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. + (instancetype)indexPathWithIndex:(NSUInteger)index;  
  2. + (instancetype)indexPathWithIndexes:(const NSUInteger [])indexes length:(NSUInteger)length;  
  3. - (instancetype)init;   /* designated initializer */  
  4. - (instancetype)initWithIndexes:(const NSUInteger [])indexes length:(NSUInteger)length; /* designated initializer */  

 --修改:注意返回的是一個新的對象,不是原來的對象基礎上改得。

[objc]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. - (NSIndexPath *)indexPathByAddingIndex:(NSUInteger)index;  
  2. - (NSIndexPath *)indexPathByRemovingLastIndex;  

--訪問屬性、比較

[objc]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. - (NSUInteger)indexAtPosition:(NSUInteger)position;  
  2. - (NSUInteger)length;  //路徑包含節點個數 或 “路徑長度"  
  3. // comparison support  
  4. - (NSComparisonResult)compare:(NSIndexPath *)otherObject;  

--例:

[objc]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. NSIndexPath *indexPath = [NSIndexPath indexPathWithIndex:1];  
  2. NSLog(@"%@  length:%D ",indexPath,indexPath.length);  //<NSIndexPath: 0x8a0ea20> {length = 1, path = 1}  length:1 ​  
  3. indexPath = [indexPath indexPathByAddingIndex:24];      
  4. NSLog(@"%@  length:%D ",indexPath,indexPath.length);  //<NSIndexPath: 0x8c54090> {length = 2, path = 1 - 24}  length:2   
  5. NSLog(@"postion:%d",[indexPath indexAtPosition:1]);  //postion:24  

 

備注:

1.在UITableview.h中定義着NSIndexPath的分類。用它來表示tableview中某section中某行,為了方便加了一些屬性和方法:

[objc]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. // This category provides convenience methods to make it easier to use an NSIndexPath to represent a section and row  
  2. @interface NSIndexPath (UITableView)      
  3. + (NSIndexPath *)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;  
  4. @property(nonatomic,readonly) NSInteger section;  
  5. @property(nonatomic,readonly) NSInteger row;  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM