在我的應用程序中,我在UItableView
的部分中顯示聯繫人列表。我的數據源是NSMutableArray
,其中包含NSDictionaries
,其中包含兩個自定義對象類型的數組(請參閱下面的debugDescription
)。Objective-C- NSPredicate-過濾NSArray包含包含NSArrays自定義對象的字典
第一部分顯示聯繫人組名(SNPgroupData.groupName
),其餘顯示按其SNPcontactData.contactName
屬性的首字母排序的聯繫人。 這一切運作良好,問題出現在我想要在tableView中搜索特定項目時。
是否可以使用NSpredicate
(或其他方法)過濾任何包含(contains[c] %@",searchText?
)搜索字符串的屬性的項目(組&)?
以下NSArray
是我UItableView
數據源:
{
Groups = (
"<SNPgroupData: 0x16e5ae80>",
"<SNPgroupData: 0x16d9ff80>"
);
},
{
A = (
"<SNPContactData: 0x16dd5fd0>",
"<SNPContactData: 0x16d8a840>",
"<SNPContactData: 0x16dd68b0>",
"<SNPContactData: 0x16dd7a10>",
"<SNPContactData: 0x16dcef60>",
"<SNPContactData: 0x16d9dc90>",
"<SNPContactData: 0x16dd6950>",
"<SNPContactData: 0x16db5070>",
"<SNPContactData: 0x16d98820>",
"<SNPContactData: 0x16dac810>",
"<SNPContactData: 0x16d8e510>",
"<SNPContactData: 0x16dbf5d0>",
"<SNPContactData: 0x16dcfbd0>"
);
},
{
B = (
"<SNPContactData: 0x16dae7b0>",
"<SNPContactData: 0x16dd6ef0>",
"<SNPContactData: 0x16dd9f90>",
"<SNPContactData: 0x16d8e480>",
"<SNPContactData: 0x16d9c750>",
"<SNPContactData: 0x16d9ba20>",
"<SNPContactData: 0x16d9ba40>",
"<SNPContactData: 0x16dd6e20>"
);
},
{
C = (
"<SNPContactData: 0x16dcf790>"
);
},
UPDATE:按照下面發現這裏的評論和信息:How to Predicate through Nested Arrays with Keys,我試圖做的線沿線的東西:
(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
NSLog(@"%@",[self.groupedAppContacts debugDescription]);
self.filteredContacts=self.groupedAppContacts;
NSPredicate *groupNamePredicate = [NSPredicate predicateWithFormat:@"ANY %K.groupName contains[c] %@",@"Groups",searchText]; //this filters out only the group objects but disregards their names
NSPredicate *contactNamePredicate = [NSPredicate predicateWithFormat:@"ANY %K.%K contactName contains[c] %@",searchText]; //this crashes
NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:
@[groupNamePredicate, contactNamePredicate]];
[self.filteredContacts filterUsingPredicate:predicate];
}
我可以從A組中篩選出組對象rray,但我得到他們所有人,而不僅僅是以我的searchText字符開頭的人。 試圖讓Predicate以不同的方式工作,但由於數組名稱對於其中的每一個都不相同,謂詞要麼不會做任何事情,要麼不會崩潰應用程序(無法解析)
不能使它可以運行兩個子謂詞,然後在主謂語兩個子謂詞返回大於零個對象要求?由於感冒,我有點頭暈,所以我可能是錯的。 –
@LeoNatan:'filteredArrayUsingPredicate'返回原始數組的「子數組」,但這裏我們需要修改元素的數組。 - 我能想到的唯一選擇是將'filterUsingPredicate'應用於內部數組,如果它們是可變的。但是這會修改原始數據源。 - 快點好起來! –
有道理。謝謝! –