0
点赞
收藏
分享

微信扫一扫

NSIndexPath

1. 创建索引路径 create indexPath object

1.1 创建一个结点的索引路径 create an one-node index path

NSIndexPath *oneNodeIndexPath = [NSIndexPath indexPathWithIndex:6];
NSLog(@"oneNodeIndexPath: %@", oneNodeIndexPath);

控制台输出:
oneNodeIndexPath: <NSIndexPath: 0x60e9> {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的话路径准确。

// 定义并初始化一个C数组:1个元素
NSUInteger indexs[] = {1};
NSIndexPath *oneNodeIndexPath = [NSIndexPath indexPathWithIndexes:indexs length:1];
NSLog(@"oneNodeIndexPath: %@", oneNodeIndexPath);
        
// 定义并初始化一个C数组:2个元素
NSUInteger indexs2[] = {1, 2};
NSIndexPath *twoNodeIndexPath = [NSIndexPath indexPathWithIndexes:indexs2 length:2];
NSLog(@"twoNodeIndexPath: %@", twoNodeIndexPath);
        
// 定义并初始化一个C数组:3个元素
NSUInteger indexs3[] = {1, 2, 3};
NSIndexPath *threeNodeIndexPath = [NSIndexPath indexPathWithIndexes:indexs3 length:3];
NSLog(@"threeNodeIndexPath: %@", threeNodeIndexPath);

控制台输出:

1.3 在tableview中的代表行索引的NSIndexPath对象创建:

NSIndexPath这个类本身在Foundation框架下,而这个方法是在UIKit下的。UIKIt里给NSIndexPath写了个category 针对于UITableView。 indexPath with two nodes

// This category provides convenience methods to make it easier to use an NSIndexPath to represent a section and row
@interface NSIndexPath (UITableView)

+ (instancetype)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;

@property (nonatomic, readonly) NSInteger section;
@property (nonatomic, readonly) NSInteger row;

@end

使用举例:

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

@interface NSIndexPath (UICollectionViewAdditions)

+ (instancetype)indexPathForItem:(NSInteger)item inSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);

@property (nonatomic, readonly) NSInteger item NS_AVAILABLE_IOS(6_0);

@end

使用举例:

NSIndexPath *indexPathForCollection = [NSIndexPath indexPathForItem:1 inSection:3];
NSLog(@"indexPathForCollection: %@", indexPathForCollection);

控制台输出:
indexPathForCollection : <NSIndexPath: 0xc000000000200316> {length = 2, path = 3 - 1}

  • 上面的都是类方法初始化出一个NSIndexPath对象,也可以用init对象方法初始化出一个NSIndexPath对象。

2. 询问索引路径 Querying Index Paths

2.1 provide the index at particular node in the index path 提供特定(指定)节点的索引值 也就是返回第几个节点的索引值

NSLog(@"index0 : %lu", (unsigned long)[threeNodeIndexPath indexAtPosition:0]);
NSLog(@"index1 : %lu", (unsigned long)[threeNodeIndexPath indexAtPosition:1]);
NSLog(@"index2 : %lu", (unsigned long)[threeNodeIndexPath indexAtPosition:2]);
NSLog(@"index3 : %lu", (unsigned long)[threeNodeIndexPath indexAtPosition:3]);

控制台输出:


可以看出传的参数position>(indexes.count-1)的话,即超过了会返回不确定值得。

2.2 给原有index path 增加一个node生成一个新的index path

// 定义并初始化一个C数组:3个元素
NSUInteger indexs3[] = {1, 2, 3};
NSIndexPath *threeNodeIndexPath = [NSIndexPath indexPathWithIndexes:indexs3 length:3];
        
NSIndexPath *newAddIndex6 = [threeNodeIndexPath indexPathByAddingIndex:6];
NSLog(@"threeNodeIndexPath: %@, newAddIndex6: %@", threeNodeIndexPath, newAddIndex6);
        
NSIndexPath *newAddIndex8 = [threeNodeIndexPath indexPathByAddingIndex:8];
NSLog(@"threeNodeIndexPath: %@, newAddIndex8: %@", threeNodeIndexPath, newAddIndex8);

控制台输出:
``

2.4 length :(索引路径的索引数组元素的个数)the number of indexs in the index path

NSUInteger len = [threeNodeIndexPath length];
NSLog(@"len: %lu", len);

控制台输出:
len :3

2.5 getIndexes:range:

拷贝存储在索引路径中的索引数组(indexes)

3. comparing

3.1 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

3.2 分别使用section和row/item

只能分别对NSIndexPath对象的section与row或item进行判断:
对于UITableView:

if (currentIndexPath.section != lastIndexPath.section || currentIndexPath.row != lastIndexPath.row) {
    // TODO
} else {
    // TODO
}

而对于UICollectionView:

if (currentIndexPath.section != lastIndexPath.section || currentIndexPath.item != lastIndexPath.item) {
    // TODO
} else {
    // TODO
}

3.3 使用NSObject的isEqual:方法

使用section和row/item的方式比较麻烦.
其实NSIndexPath对象可以通过NSObject的isEqual:方法来进行比较, 实际上比较的是二者的hash值. 因此跟二者的内存地址无关.

if (![currentIndexPath isEqual:lastIndexPath]) {
    // TODO
}

两个NSIndexPath对象的hash值相等, 因此isEqual:的结果为YES.
同样, 对应NSString, NSArray, NSDictionary等对象, 除了各自的isEqualToString, isEqualToArray, isEqualToDictionary之外, 还可以使用isEqual:进行比较, 同样比较的是其hash值.

  • 错误写法
if (currentIndexPath != lastIndexPath) {
    // TODO
} else {
    // TODO
}
举报

相关推荐

0 条评论