NSIndexSet-入門淺析

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



記得上一次,用到,關於刪除UITableView分組的方法

[tableViewdeleteSections:[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)rangeoptions:(NSEnumerationOptions)optsusingBlock:(void (^)(NSUInteger idx, BOOL *stop))block

如果,要枚舉的NSIndexSet中不存在Rang中所指定的範圍,則跳過。


options參數:

enum {

  NSEnumerationConcurrent = (1UL<< 0),

  NSEnumerationReverse = (1UL <<1),

};

typedef NSUIntegerNSEnumerationOptions;


NSEnumerationConcurrent

枚舉過程中,各個Block是同時開始執行的。這樣枚舉的完成順序是不確定的。


NSEnumerationReverse

以反序方式枚舉。


例子:


 

// theTwo中是否包含theOne

    BOOL isContains1= [theTwo containsIndexes:theOne];

    BOOL isContains2= [theTwo containsIndex:1];

    BOOL isContains3= [theTwo containsIndex:9];

   // theTwo中是否包含指定的NSMakeRange

   BOOLisContains4= [theTwocontainsIndexesInRange: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);

                     }];


發佈了7 篇原創文章 · 獲贊 0 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章