【发布时间】:2009-08-19 19:42:24
【问题描述】:
(注意:这是previous question的扩展。)
我在为相当复杂的 tableview 实现 SearchBar 时遇到了一些困难。 tableView 有多个部分,两行文本和一个图像。所有数据都从 plist 加载,然后按“名称”键值的首字母放入部分中。 NSLog 为我的主字典“sectionedDictionaryByFirstLetter”返回以下内容:
sectionedDictionaryByFirstLetter:{
B = (
{
Name = "B...A Name Starting with B";
Image = "ImageName1.png";
Text = "Some Text";
}
);
C = (
{
Name = "C...A Name Starting with C";
Image = "ImageName2.png";
Text = "Some Text";
}
);
N = (
{
Name = "N1...A Name Starting with N";
Image = "ImageName3.png";
Text = "Some Text";
},
{
Name = "N2...A Name Starting with N";
Image = "ImageName4.png";
Text = "Some Text";
},
{
Name = "N3...A Name Starting with N";
Image = "ImageName5.png";
Text = "Some Text";
}
);
}
我想按“名称”键的值过滤子词典。任何人都可以推荐如何进行此操作或一些学习方法的资源吗?我一直在遵循“开始 iPhone 3 开发”中的示例,但无法将其转化为我的情况。 因此,如果用户在搜索栏中键入“with N”,我希望新字典显示为:
filteredSectionedDictionaryByFirstLetter:{
N = (
{
Name = "N1...A Name Starting with N";
Image = "ImageName3.png";
Text = "Some Text";
},
{
Name = "N2...A Name Starting with N";
Image = "ImageName4.png";
Text = "Some Text";
},
{
Name = "N3...A Name Starting with N";
Image = "ImageName5.png";
Text = "Some Text";
}
);
}
澄清一下,我在视图中安装了我的 searchBar,我只是无法使其正常工作。我目前正在使用书中示例中称为 - (void)handleSearchForTerm:(NSString *)searchTerm 的自定义方法。
感谢您的帮助!
这是数据的一般结构。
sectionedDictionaryByFirstLetter
Key Value
B Array of Dictionaries with Names beginning with B
....
C Array of Dictionaries with Names beginning with C
...
N Array of Dictionaries with Names beginning with N
Item 1 Dictionary 1
Item 2 Dictionary 2
Item 3 Dictionary 3
Dictionary 3
{
Name = "N3...A Name Starting with N";
Image = "ImageName5.png";
Text = "Some Text";
}
我希望这会有所帮助。我仍在拼命寻找这个问题的答案。
本质上,有一个主字典,它的键是它包含的项目的第一个字母。在每个第一个字母中,都有一个字典数组,它们的“名称”键值以该字母开头。
【问题讨论】:
-
查看我的答案,但您不想为此使用字典列表。你想创建一个 NSObject 子类,语法不会那么糟糕。
标签: iphone arrays dictionary filter