昂首千丘遠,嘯傲風(fēng)間,堪尋敵手共論劍,高處不勝寒。
——風(fēng)之痕
NSIndexPath
索引列表,它們一起表示嵌套數(shù)組樹中特定位置的路徑。

創(chuàng)建索引路徑
//創(chuàng)建空索引
NSIndexPath *indexPath = [[NSIndexPath alloc]init];//輸出:{length = 0, path = }
//創(chuàng)建單節(jié)點索引路徑
NSIndexPath *indexPath1 = [NSIndexPath indexPathWithIndex:1];//輸出:{length = 1, path = 1}
//創(chuàng)建具有一個或多個節(jié)點的索引路徑。
NSUInteger indexs[] = {1,2,3,4,5};
NSIndexPath *indexPath2 = [NSIndexPath indexPathWithIndexes:indexs length:5];//輸出:{length = 5, path = 1 - 2 - 3 - 4 - 5}
更改索引路徑
//索引添加新節(jié)點,返回新的索引
NSIndexPath *indexPath3 = [indexPath1 indexPathByAddingIndex:2];//輸出:{length = 2, path = 1 - 2}
//移除索引尾部節(jié)點,返回新的索引
NSIndexPath *indexPath4 = [indexPath2 indexPathByRemovingLastIndex];//輸出:{length = 4, path = 1 - 2 - 3 - 4}
//將索引路徑中存儲的索引從位置范圍指定的位置復(fù)制到指定的索引中
[indexPath2 getIndexes:indexs range:NSMakeRange(0, indexPath2.length-2)];//輸出:{length = 5, path = 1 - 2 - 3 - 4 - 5}
訪問索引路徑值
//返回索引指定節(jié)點的值
NSUInteger index = [indexPath2 indexAtPosition:1];//輸出:2
//返回索引路徑中的節(jié)點數(shù)
NSUInteger count = indexPath2.length;//輸出:5
比較索引
//比較索引
/*
typedef NS_CLOSED_ENUM(NSInteger, NSComparisonResult) {
NSOrderedAscending = -1L,升序
NSOrderedSame,相等
NSOrderedDescending 降序
};
*/
NSComparisonResult result = [indexPath1 compare:indexPath2];//輸出:NSOrderedAscending
NSIndexPath (UIKitAdditions)
UIKIt框架下的索引方法,針對于UITableView與UICollectionView
UITableView-索引路徑創(chuàng)建與訪問
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];//輸出:{length = 2, path = 0 - 0}
NSInteger section = indexPath.section;//0
NSInteger row = indexPath.row;//0
UICollectionView-索引路徑創(chuàng)建與訪問
NSIndexPath *indexPath1 = [NSIndexPath indexPathForItem:0 inSection:0];//輸出:{length = 2, path = 0 - 0}
NSInteger item = indexPath1.item;//0