2012-02-28 79 views
0

SETUP從的NSMutableDictionary獲取對象和UITableView的

顯示出來我有NSMutableDictionaries的一個NSMutableDictionary。我創建了搜索術語的字典,現在我想顯示它們。我使用了一種方法(如下)將包含搜索項的項目複製到另一個字典中。我知道它正在複製,因爲我可以打印字典並查看結果。同時顯示正確數量的行,但不顯示數據。我已經包含了複製字典的方法,以及字典在原始數組中的樣子以及它在新數組中的外觀。

問題

我拉項目從NSDictionaires的理解是不正確的。我已經閱讀了多個地方,我知道我想使用objectforkey。但我無法弄清楚如何從主要字典中獲取字典並顯示它們。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
static NSString *CellIdentifier = @"Cell"; 


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
}  
if (searching) { 


//ATTEMPT ONE 
//  NSDictionary *searchEmp = [copyListOfItems objectForKey:[copyListOfItems allkeys]];  
//  cell.textLabel.text = [[searchEmp objectForKey:kFULLNAME_TAG] objectAtIndex:indexPath.row];    

//ATTEMPT TWO  
    NSString *name = [[copyListOfItems allKeys] objectAtIndex: indexPath.row]; 
    NSString *value = [copyListOfItems objectForKey:path]; 

    cell.textLabel.text = [copyListOfItems objectForKey:value]; 




}else{ 

    NSDictionary *employee = [[self.orderedSections valueForKey:[[[self.orderedSections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]; 

cell.textLabel.text = [employee objectForKey:kFULLNAME_TAG]; 
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
} 
return cell; 

}

這是我用於複製從一個的NSMutableDictionary到另一個

for (NSDictionary *emp in employeeData) 
{ 
    NSString *empName = [emp objectForKey:kFULLNAME_TAG]; 
    NSRange titleResultsRange = [empName rangeOfString:searchText options:NSCaseInsensitiveSearch]; 

    if (titleResultsRange.length > 0){ 
     NSLog(@"search result ---> %@" ,emp); 
     [copyListOfItems setValue:emp forKey:empName]; 
    } 

} 

這是的NSMutableDictionary之前,我將其複製到了 「copyListofItems」

2 012-02-27 17:24:44.399 USSContacts[14514:10d03] search result ---> { 
0 = JO; 
1 = "Zary Joon"; 
10 = "N/A"; 
11 = "N/A"; 
2 = Joon; 
3 = Zary; 
4 = "N/A"; 
5 = "N/A"; 
6 = "N/A"; 
7 = "[email protected]"; 
8 = "N/A"; 
9 = "N/A"; 
BUID = "N/A"; 
Department = "N/A"; 
EmployeeId = JONZ; 
Extension = "N/A"; 
FirstName = Zary; 
FullName = "Zachary Joon"; 
LastName = Joon; 
Mobile = "N/A"; 
Shift = "N/A"; 
Supervisor = "N/A"; 
Title = "N/A"; 
email = "[email protected]"; 
該方法

}

我試圖在UITableView中顯示的字典看起來像這樣。它是上述的副本。

<CFBasicHash 0x5c6aa10 [0x1175400]>{type = mutable dict, count = 1, 

2 : <CFString 0x5c7cf20 [0x1175400]>{contents = "Zary Joon"} = <CFBasicHash 0x5c7d0a0 [0x1175400]>{type = mutable dict, count = 24, 
0 : <CFString 0x7058790 [0x1175400]>{contents = "Shift"} = <CFString 0x7059970 [0x1175400]>{contents = "N/A"} 
1 : <CFString 0x7054390 [0x1175400]>{contents = "Mobile"} = <CFString 0x7059970 [0x1175400]>{contents = "N/A"} 
2 : <CFString 0x70543c0 [0x1175400]>{contents = "Title"} = <CFString 0x7059970 [0x1175400]>{contents = "N/A"} 
4 : <CFString 0x7058ed0 [0x1175400]>{contents = "10"} = <CFString 0x7059970 [0x1175400]>{contents = "N/A"} 
6 : <CFString 0x7054330 [0x1175400]>{contents = "Supervisor"} = <CFString 0x7059970 [0x1175400]>{contents = "N/A"} 
8 : <CFString 0x7058100 [0x1175400]>{contents = "FirstName"} = <CFString 0x5c7d2a0 [0x1175400]>{contents = "Zary"} 
9 : <CFString 0x7058f80 [0x1175400]>{contents = "11"} = <CFString 0x7059970 [0x1175400]>{contents = "N/A"} 
13 : <CFString 0x7051a70 [0x1175400]>{contents = "0"} = <CFString 0x5c7c100 [0x117500]>{contents = "J"} 
14 : <CFString 0x7054370 [0x1175400]>{contents = "email"} = <CFString 0x5c7d2c0 [0x1175400]>{contents = "[email protected]"} 
15 : <CFString 0x7059ba0 [0x1175400]>{contents = "BUID"} = <CFString 0x7059970 [0x1175400]>{contents = "N/A"} 
16 : <CFString 0x7058120 [0x1175400]>{contents = "1"} = <CFString 0x5c7cf20 [0x1175400]>{contents = "Zary Joon"} 
19 : <CFString 0x7058c40 [0x1175400]>{contents = "2"} = <CFString 0x7063630 [0x1175400]>{contents = "Joon"} 
22 : <CFString 0x7059b60 [0x1175400]>{contents = "3"} = <CFString 0x5c7d2a0 [0x1175400]>{contents = "Zary"} 
23 : <CFString 0x7059bc0 [0x1175400]>{contents = "Department"} = <CFString 0x7059970 [0x1175400]>{contents = "N/A"} 
25 : <CFString 0x7059960 [0x1175400]>{contents = "4"} = <CFString 0x7059970 [0x1175400]>{contents = "N/A" 
26 : <CFString 0x7058770 [0x1175400]>{contents = "LastName"} = <CFString 0x7063630 [0x1175400]>{contents = "Joon"} 
28 : <CFString 0x70527b0 [0x1175400]>{contents = "5"} = <CFString 0x7059970 [0x1175400]>{contents = "N/A"} 
30 : <CFString 0x7058750 [0x1175400]>{contents = "FullName"} = <CFString 0x5c7cf20 [0x1175400]>{contents = "Zary Joon"} 
31 : <CFString 0x70527c0 [0x1175400]>{contents = "6"} = <CFString 0x7059970 [0x1175400]>{contents = "N/A"} 
34 : <CFString 0x70527f0 [0x1175400]>{contents = "7"} = <CFString 0x5c7d2c0 [0x1175400]>{contents = "[email protected]"} 
36 : <CFString 0x70543a0 [0x1175400]>{contents = "EmployeeId"} = <CFString 0x5c7c100 [0x1175400]>{contents = "J"} 
37 : <CFString 0x70563a0 [0x1175400]>{contents = "8"} = <CFString 0x7059970 [0x1175400]>{contents = "N/A"} 
38 : <CFString 0x70580e0 [0x1175400]>{contents = "Extension"} = <CFString 0x7059970 [0x1175400]>{contents = "N/A"} 
40 : <CFString 0x70563c0 [0x1175400]>{contents = "9"} = <CFString 0x7059970  [0x1175400]>{contents = "N/A"} 
} 

任何人都可以解釋爲什麼它具有複製後的所有附加信息?

回答

5

你在正確的軌道上。要使用主數據庫NSDictionary作爲您的表視圖的數據源,您需要將其轉換爲數組,您可以通過調用allKeys來完成此操作。這將爲您提供主字典中項目的所有鍵的數組。以正確的索引獲取項目,然後將其用作從主字典中提取項目的關鍵字。

您提取的項目本身就是一本字典,因此您可以使用objectForKey:來提取想要在表格單元格中顯示的任何值。

NSString *name = [[copyListOfItems allKeys] objectAtIndex: indexPath.row]; 
NSDictionary *employeeDict = [copyListOfItems objectForKey:name]; 

cell.textLabel.text = [employeeDict objectForKey:@"FullName"]; 
+0

我想你可能已經回答了我的一些問題,順便說一下,謝謝了TON。這終於有道理!謝謝一堆 – zach 2012-02-28 03:45:28

0

[copyListOfItems allKeys]的順序不保證每次都是一樣的,但更重要的是,不要擔心所看到的額外信息。具體的子類(即CFString而不是NSString)對你來說是不透明的;你不必擔心它似乎有更多的信息。

我想你可能想使用類似

NSString *value = [copyListOfItems objectForKey:[NSString stringWithFormat:@"%lu", (unsigned long) indexPath.row]]; 

這樣至少你的關鍵是數量,這就是我通過11假定0是。你必須找到一個不同的方式來獲得你的部門,全名(等等),但其他領域。

+0

我已經閱讀了一些地方,有一種方法來按照與列表相同的方式對按鍵進行字母順序排列......你是否熟悉我在說什麼? – zach 2012-02-28 03:44:36

+0

是的 - 你想要'[myArray sortedArrayUsingDescriptors:arrayOfSortDescriptors]'。在系列管中有很多示例:) – 2012-02-28 12:33:31