iOS官方文檔 Foundation篇---NSIndexPath

昂首千丘遠,嘯傲風(fēng)間,堪尋敵手共論劍,高處不勝寒。

——風(fēng)之痕

NSIndexPath

索引列表,它們一起表示嵌套數(shù)組樹中特定位置的路徑。

索引路徑中的每個索引表示從樹中的一個節(jié)點到另一個更深的節(jié)點的子數(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
歡迎留言指正,會持續(xù)更新。。。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容