NSIndexSet-入门浅析

http://blog.sina.com.cn/s/blog_7b9d64af0101b851.html

记得上一次,用到,关于删除UITableView分组的方法
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationLeft];


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


1.NSIndexSet是什么?


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


2.NSIndexSet的一些常用方法。


类方法:


创建一个空的索引集合。
+ (id)indexSet


创建一个索引集合,根据索引值
+ (id)indexSetWithIndex:(NSUInteger)index


创建一个索引集合,根据一个NSRange对象
+ (id)indexSetWithIndexesInRange:(NSRange)indexRange




实例方法:


判断索引集合中是否包含制定的索引值
- (BOOL)containsIndex:(NSUInteger)index


判断索引集合是否包含指定的indexSet
- (BOOL)containsIndexes:(NSIndexSet *)indexSet


判断索引集合是否包含指定的indexRange
- (BOOL)containsIndexesInRange:(NSRange)indexRange


返回索引集合包含的索引数量
- (NSUInteger)count


返回indexRange中包含的索引数量
- (NSUInteger)countOfIndexesInRange:(NSRange)indexRange


枚举NSIndexSet;执行Block操作,在指定的Rang范围内,并使用指定的options方法。
- (void)enumerateIndexesInRange:(NSRange)range options:(NSEnumerationOptions)opts usingBlock:(void (^)(NSUInteger idx, BOOL *stop))block
如果,要枚举的NSIndexSet中不存在Rang中所指定的范围,则跳过。


options参数:
enum {
   NSEnumerationConcurrent = (1UL << 0),
   NSEnumerationReverse = (1UL << 1),
};
typedef 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);
                         }];


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值