2010-09-22 28 views
12

tôi nhận được các mục chọn từ một cái nhìn bảng với:Làm cách nào để lấy chỉ mục từ NSIndexset vào NSArray trong ca cao?

NSIndexSet *selectedItems = [aTableView selectedRowIndexes]; 

cách tốt nhất để có được các chỉ số trong một đối tượng NSArray là gì?

+1

Tại sao bạn muốn thực hiện việc này? – Chuck

+0

tôi có một phương thức nhận được một NSArray làm tham số để xóa nhiều bản ghi được chọn từ tableview –

+6

Điều đó có vẻ hơi tròn. Nếu mục đích của mảng là chứa một tập hợp các chỉ mục, tại sao không có phương thức thay thế một NSIndexSet? – Chuck

Trả lời

20

Liệt kê tập hợp, đặt NSNumbers ra khỏi các chỉ mục, thêm NSNumbers vào một mảng.

Đó là cách bạn thực hiện. Tôi không chắc rằng tôi thấy điểm trong việc chuyển đổi một tập hợp các chỉ mục thành một biểu diễn kém hiệu quả hơn.

Để liệt kê một tập hợp, bạn có hai tùy chọn. Nếu bạn đang nhắm mục tiêu OS X 10.6 hoặc iOS 4, bạn có thể sử dụng enumerateIndexesUsingBlock:. Nếu bạn đang nhắm mục tiêu các phiên bản trước đó, bạn sẽ phải nhận được firstIndex và sau đó tiếp tục yêu cầu indexGreaterThanIndex: về kết quả trước đó cho đến khi bạn nhận được NSNotFound.

+0

bạn nói đúng là tôi không biết cách lấy chỉ mục từ NSIndexSet bằng vòng lặp while hoặc vòng lặp foreach .... –

+5

Nó nằm trong tài liệu: http://developer.apple.com/library /mac/#documentation/Cocoa/Conceptual/Collections/Articles/index.html%23//apple_ref/doc/uid/TP40010165-SW4 – Wevah

+0

@Wevah người đàn ông hoàn hảo !!! –

11
NSIndexSet *selectedItems = [aTableView selectedRowIndexes]; 

NSMutableArray *selectedItemsArray=[NSMutableArray array]; 
    [selectedItems enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) { 
     [selectedItemsArray addObject:[NSNumber numberWithInteger:idx]]; 
    }]; 
3

Với nhanh chóng bạn có thể làm như sau

extension NSIndexSet { 
    func toArray() -> [Int] { 
     var indexes:[Int] = []; 
     self.enumerateIndexesUsingBlock { (index:Int, _) in 
      indexes.append(index); 
     } 
     return indexes; 
    } 
} 

sau đó bạn có thể làm

selectedItems.toArray() 
+0

Bạn có thể xóa hoặc cập nhật câu trả lời này không? Có một cách tốt hơn trong Swift: http://stackoverflow.com/a/37978236/3141234 – Alexander

1

tôi đã làm nó bằng cách tạo ra một thể loại trên NSIndexSet. Điều này giữ nó nhỏ và hiệu quả, đòi hỏi rất ít mã trên một phần của tôi.

giao diện của tôi (NSIndexSet_Arrays.h):

/** 
* Provides a category of NSIndexSet that allows the conversion to and from an NSDictionary 
* object. 
*/ 
@interface NSIndexSet (Arrays) 

/** 
* Returns an NSArray containing the contents of the NSIndexSet in a format that can be persisted. 
*/ 
- (NSArray*) arrayRepresentation; 

/** 
* Initialises self with the indexes found wtihin the specified array that has previously been 
* created by the method @see arrayRepresentation. 
*/ 
+ (NSIndexSet*) indexSetWithArrayRepresentation:(NSArray*)array; 

@end 

và thực hiện (NSIndexSet_Arrays.m):

#import "NSIndexSet_Arrays.h" 

@implementation NSIndexSet (Arrays) 

/** 
* Returns an NSArray containing the contents of the NSIndexSet in a format that can be persisted. 
*/ 
- (NSArray*) arrayRepresentation { 
    NSMutableArray *result = [NSMutableArray array]; 

    [self enumerateRangesUsingBlock:^(NSRange range, BOOL *stop) { 
     [result addObject:NSStringFromRange(range)]; 
    }]; 

    return [NSArray arrayWithArray:result]; 
} 

/** 
* Initialises self with the indexes found wtihin the specified array that has previously been 
* created by the method @see arrayRepresentation. 
*/ 
+ (NSIndexSet*) indexSetWithArrayRepresentation:(NSArray*)array { 
    NSMutableIndexSet *result = [NSMutableIndexSet indexSet]; 

    for (NSString *range in array) { 
     [result addIndexesInRange:NSRangeFromString(range)]; 
    } 

    return result; 
} 


@end 
0

Đây là đoạn mã mẫu:

NSIndexSet *filteredObjects = [items indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {do testing here}]; 

NSArray *theObjects = [theItems objectsAtIndexes:filteredObjects] 

Availability Khả dụng trong iOS 2.0 trở lên.

Các vấn đề liên quan