2011-06-28 139 views
0

我有一個NSMutableArray的具有下列對象:NSMutableArray的快速搜索????/

@interface MyViewCell : UITableViewCell { 

    NSUInteger id; 
    ..... 
    } 

在一些方法,我需要與預定義的ID的小區快速搜索。如何最好的做到這一點?

回答

9

可能最簡單的方法是使用- (NSUInteger)indexOfObjectPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate。請參閱文檔here

int index = [myArray indexOfObjectPassingTest: ^(id obj, NSUInteger idx, BOOL *stop) { 
    MyViewCell *cell = (MyViewCell *)obj; 
    BOOL result = (cell.id == someValue); 
    stop = &result; 
    return result; 
}]; 
+0

謝謝。我怎樣才能找到MyViewCell?據我所知它只是搜索預定義的ID。 –

+0

'[myArray objectAtIndex:index]' – highlycaffeinated

1

當我嘗試代碼時,遇到了一個小問題。下一個調整代碼適合我(* stop):

int index = [myArray indexOfObjectPassingTest: ^(id obj, NSUInteger idx, BOOL *stop) { 
    MyViewCell *cell = (MyViewCell *)obj; 
    BOOL result = (cell.id == someValue); 
    *stop = result; 
    return result; 
}]; 
+0

是的,這是一個小錯誤:) –