从NSMutableDictionary获取对象并在UITableView中显示它

时间:2012-02-28 01:16:31

标签: ios uitableview ios4 nsmutabledictionary

SETUP

我有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];
    }

}

在将其复制到“copyListofItems”

之前,这是NSMutableDictionary
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 = "zjoon@usa.com";
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 = "zjoon@usa.com";

}

我试图在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 = "zjo@usa.com"}
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 = "zjoon@usa.com"}
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"}
}

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

2 个答案:

答案 0 :(得分:5)

你走在正确的轨道上。要将主NSDictionary用作表格视图的数据源,您需要将其转换为数组,您可以通过调用allKeys来执行此操作。这将为您提供主词典中项目的所有键的数组。获取正确索引处的项目,然后将其用作从主词典中提取项目的键。

您提取的项目本身就是一个字典,因此您可以使用objectForKey:提取要在表格单元格中显示的任何值。

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

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

答案 1 :(得分:0)

[copyListOfItems allKeys]的顺序不保证每次都相同,但更重要的是,不要担心您看到的额外信息。具体的子类(即CFString而不是NSString)对您来说是不透明的;你不必担心它似乎有更多的信息。

我想你可能想要使用像

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

这样至少你的关键是数字,这就是我假设0到11的数字。你必须找到一种不同的方式来获取你的部门,全名(等等)其他领域。

相关问题