昂首千丘远,啸傲风间,堪寻敌手共论剑,高处不胜寒。
——风之痕
NSIndexPath
索引列表,它们一起表示嵌套数组树中特定位置的路径。
//创建空索引
NSIndexPath *indexPath = [[NSIndexPath alloc]init];//{length = 0, path = }
//创建单节点索引路径
NSIndexPath *indexPath1 = [NSIndexPath indexPathWithIndex:1];//{length = 1, path = 1}
//创建具有一个或多个节点的索引路径。
NSUInteger indexs[] = {1,2,3,4,5};
NSIndexPath *indexPath2 = [NSIndexPath indexPathWithIndexes:indexs length:5];//{length = 5, path = 1 - 2 - 3 - 4 - 5}
//索引添加新节点,返回新的索引
NSIndexPath *indexPath3 = [indexPath1 indexPathByAddingIndex:2];//{length = 2, path = 1 - 2}
//移除索引尾部节点,返回新的索引
NSIndexPath *indexPath4 = [indexPath2 indexPathByRemovingLastIndex];//{length = 4, path = 1 - 2 - 3 - 4}
//返回索引指定节点的值
NSUInteger index = [indexPath2 indexAtPosition:1];//2
//返回索引路径中的节点数
NSUInteger count = indexPath2.length;//5
//将索引路径中存储的索引从位置范围指定的位置复制到指定的索引中
[indexPath2 getIndexes:indexs range:NSMakeRange(0, indexPath2.length-2)];//{length = 5, path = 1 - 2 - 3 - 4 - 5}
//比较索引
NSComparisonResult result = [indexPath1 compare:indexPath2];
NSIndexPath (UIKitAdditions)
UIKIt框架下的索引方法,针对于UITableView与UICollectionView
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSIndexPath *indexPath1 = [NSIndexPath indexPathForItem:0 inSection:0];
NSInteger section = indexPath.section;//0
NSInteger row = indexPath.row;//0
NSInteger item = indexPath1.item;//0
网友评论