NSIndexSet-入门浅析

  • 关于删除UITableView分组的方法
    [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]withRowAnimation:UITableViewRowAnimationLeft];
  • tableView 刷新分区的方法:

        [tableView reloadSections:[NSIndexSet           indexSetWithIndex:indexPath.section]withRowAnimation:UITableViewRowAnimationFade];
    

确实,搞不懂NSIndexSet是啥东东!不懂就看呗!
在这里,整理了一下常用方法。高手绕道!多多指点!

1.****NSIndexSet****是什么?

NSIndexSet 是个无符号整数集合。集合中的元素不可变的、不可重复。常被用来当作索引使用。就从它字面上理解,就叫做:索引集合。

2.NSIndexSet的一些常用方法。

类方法:

创建一个空的索引集合。

+ (id)indexSet

创建一个索引集合,根据索引值

+ (id)indexSetWithIndex:([NSUInteger](file:///Users/zhangzhen/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.AppleiOS6.0.iOSLibrary.docset/Contents/Resources/Documents/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html#//apple_ref/doc/c_ref/NSUInteger))index

创建一个索引集合,根据一个NSRange对象

  • (id)indexSetWithIndexesInRange:([NSRange](file:///Users/zhangzhen/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.AppleiOS6.0.iOSLibrary.docset/Contents/Resources/Documents/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html#//apple_ref/doc/c_ref/NSRange))indexRange

实例方法:

判断索引集合中是否包含制定的索引值

  • (BOOL)containsIndex:([NSUInteger](file:///Users/zhangzhen/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.AppleiOS6.0.iOSLibrary.docset/Contents/Resources/Documents/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html#//apple_ref/doc/c_ref/NSUInteger))index

判断索引集合是否包含指定的indexSet

- (BOOL)containsIndexes:(NSIndexSet *)indexSet

判断索引集合是否包含指定的indexRange

- (BOOL)containsIndexesInRange:([NSRange](file:///Users/zhangzhen/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.AppleiOS6.0.iOSLibrary.docset/Contents/Resources/Documents/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html#//apple_ref/doc/c_ref/NSRange))indexRange

返回索引集合包含的索引数量

- ([NSUInteger](file:///Users/zhangzhen/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.AppleiOS6.0.iOSLibrary.docset/Contents/Resources/Documents/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html#//apple_ref/doc/c_ref/NSUInteger))count

返回indexRange中包含的索引数量

 - ([NSUInteger](file:///Users/zhangzhen/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.AppleiOS6.0.iOSLibrary.docset/Contents/Resources/Documents/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html#//apple_ref/doc/c_ref/NSUInteger))countOfIndexesInRange:([NSRange](file:///Users/zhangzhen/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.AppleiOS6.0.iOSLibrary.docset/Contents/Resources/Documents/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html#//apple_ref/doc/c_ref/NSRange))indexRange

枚举NSIndexSet;执行Block操作,在指定的Rang范围内,并使用指定的options方法。

  • (void)enumerateIndexesInRange:([NSRange](file:///Users/zhangzhen/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.AppleiOS6.0.iOSLibrary.docset/Contents/Resources/Documents/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html#//apple_ref/doc/c_ref/NSRange))range options:([NSEnumerationOptions](file:///Users/zhangzhen/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.AppleiOS6.0.iOSLibrary.docset/Contents/Resources/Documents/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html#//apple_ref/doc/c_ref/NSEnumerationOptions))opts usingBlock:(void (^)(NSUInteger idx, BOOL *stop))block
    如果,要枚举的NSIndexSet中不存在Rang中所指定的范围,则跳过。

options参数:

    enum {
       NSEnumerationConcurrent = (1UL << 0),
       NSEnumerationReverse = (1UL << 1),
    };

typedef [NSUInteger](file:///Users/zhangzhen/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.AppleiOS6.0.iOSLibrary.docset/Contents/Resources/Documents/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html#//apple_ref/doc/c_ref/NSUInteger) NSEnumerationOptions;

NSEnumerationConcurrent
枚举过程中,各个Block是同时开始执行的。这样枚举的完成顺序是不确定的。

NSEnumerationReverse
以反序方式枚举。

例子:


// theTwo中是否包含theOne

        BOOL isContains1= [theTwo containsIndexes:theOne];
        BOOL isContains2= [theTwo containsIndex:1];
        BOOL isContains3= [theTwo containsIndex:9];
// theTwo中是否包含指定的NSMakeRange
BOOL isContains4= [theTwo containsIndexesInRange:NSMakeRange(0,5)];
int theCount=[theTwo count];

// 遍历theTwo,在指定的Range范围内,执行Block方法,利用制定的options方式
// 如果,theTwo中不存在Range中所指定的范围,在theTwo中不存在,则跳过。

  [theTwo enumerateIndexesInRange:NSMakeRange(0,8)  options:NSEnumerationReverse
                     usingBlock:^(NSUInteger idx, BOOL *stop) {
    
                         NSLog(@"-------%d",idx);
                         NSLog(@"%@",theTwo);
                     }];

其他的方法,都差不多,就不一一介绍了!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容