从的NSMutableDictionary获取对象和UITableView的

问题描述:

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"} 
} 

任何人都可以解释为什么它具有复制后的所有附加信息?

你在正确的轨道上。要使用主数据库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

[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