2010-10-27 70 views
0

我有一個包含字典的數組,我想根據每個字典中的「名稱」字段在表視圖中顯示值。假設名字以「a」開頭;那麼「b」和「c」在不同的部分。部分的數量對應於字典的「名稱」字段。我如何獲得行數?如果有人知道,請給解決方案。NSPredicate協助數組中的對象

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    //---get the letter in each section; e.g., A, B, C, etc.--- 
    NSString *alphabet = [subscribeIndexArray objectAtIndex:section]; 

    //---get all states beginning with the letter--- 

    //NSMutableArray *states =[[NSMutableArray alloc]init]; 
    NSArray *states; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet]; 

    for(NSDictionary *dict in subscribeViewArray1) 
    { 
     states =[[dict valueForKey:@"name"] filteredArrayUsingPredicate:predicate]; 
    } 

    //---return the number of states beginning with the letter--- 
    return [states count]; 
} 

回答

1

您需要在數組中包含狀態名以便用謂詞過濾它們。

試試這個:

... 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet]; 
NSMutableArray *matchingStatesArray = [[NSMutableArray alloc] init]; 
for (NSDictionary *dict in subscribeViewArray1) { 
     //get the state names in an array 
    [matchingStatesArray addObject:[dict objectForKey:@"name"]]; 
    } 
    [matchingStatesArray filterUsingPredicate:predicate]; 

    return [[matchingStatesArray autorelease] count]; 
} 
+0

我已經這樣做了,但我想你separting與名稱值數組整部字典 – parvind 2010-10-30 05:58:14