ios8 – 嵌套对象IOS的NSPredicate问题

ios8 – 嵌套对象IOS的NSPredicate问题,第1张

概述如何在“self.tableInfo”(NSMutableArray)上使用NSPredicate来使用具有以下结构的谓词键“name”来获取CheckIn的对象.由于self.tableInfo有一个SectionInfo数组,每个SectionInfo都有自己的单元格(CellInfo),每个单元格都有checkin对象. - (void)configureView { self. 如何在“self.tableInfo”(NSMutableArray)上使用nspredicate来使用具有以下结构的谓词键“name”来获取CheckIn的对象.由于self.tableInfo有一个SectionInfo数组,每个SectionInfo都有自己的单元格(Cellinfo),每个单元格都有checkin对象.

- (voID)configureVIEw   {    self.tableInfo = [NSMutableArray alloc]init];    self.filteredtableInfo = [NSMutableArray alloc]init];    NSArray *checkIns =  [CheckIn MR_findAll];    SectionInfo*checkInSection = [SectionInfo section];    for (CheckIn *checkIn in checkIns) {        Cellinfo *ListCell = (Cellinfo *)[Cellinfo cell];        ListCell.classname = NsstringFromClass([FeedListCell class]);        ListCell.height = 260;        ListCell.object = checkIn;        [checkInSection.cells addobject:ListCell];    }    if ([checkIns count] > 0) {        self.tableInfo = [NSMutableArray arrayWithArray:@[checkInSection]];        self.filteredtableInfo = self.tableInfo;        [self.tableVIEw setHIDden:NO];        [self.tableVIEw reloadData];    }}- (NSInteger)numberOfSectionsIntableVIEw:(UItableVIEw *)tableVIEw {    if (self.tableInfo) {        return self.tableInfo.count;    }    return 0;}- (NSInteger)tableVIEw:(UItableVIEw *)tableVIEw numberOfRowsInSection:(NSInteger)section {    if (self.tableInfo && (section < self.tableInfo.count) ) {        SectionInfo *sectionInfo = [self.tableInfo objectAtIndex:section];        return sectionInfo.numberOfCells;    }    return 1;}- (UItableVIEwCell *)tableVIEw:(UItableVIEw *)tableVIEw cellForRowAtIndexPath:(NSIndexPath *)indexPath {    SectionInfo *sectionInfo = self.tableInfo[indexPath.section];    Cellinfo *formInfoCell = sectionInfo.cells[indexPath.row];    CheckIn *checkin = formInfoCell.object;    NSLog(@"%@",checkIn.name);}

我使用的方式是这样的:

- (voID)searchbar:(UISearchbar *)searchbar textDIDChange:(Nsstring *)searchText {    if([searchText length] != 0) {        [self filterContentForSearchText:searchText];    }}- (voID)filterContentForSearchText:(Nsstring *)searchText   {    nspredicate *namePredicate = [nspredicate predicateWithFormat:@"ANY cells.object.name contains[c] %@",searchText];    NSArray * filteredArray = [self.filteredtableInfo filteredArrayUsingPredicate:namePredicate];    self.tableInfo = [NSMutableArray arrayWithArray:@[filteredArray]];    [self.tableVIEw reloadData];}

这使我总是零计数.处理这件事有什么好主意吗?

解决方法 看起来你对这个谓词有几个问题.首先,在您的谓词中,您正在基于“Cellinfo.object”进行搜索.SELF.cells.object包含[c]%@而不是“Cellinfo.object.name”,就像您希望的那样.

其次,当在数组中搜索数组时(在您的tableInfo数组中的SectionInfo.cells中),您不能简单地使用点表示法来搜索内部数组.您必须在数组中指定与查询匹配的项目.您可以使用其中一个关键字ANY ALL或NONE来完成此 *** 作.

例如,要获取任何具有其CheckIn.name包含搜索文本的Cellinfo的SectionInfo,您需要执行此 *** 作:

nspredicate *namePredicate = [nspredicate predicateWithFormat:@"ALL cells.object.name contains[c] %@",searchText"];NSArray *filteredSections = [self.tableInfo filteredArrayUsingPredicate:namePredicate]

但是,这将返回一个SectionInfo数组,而不是您想要的CheckIn对象.要获取CheckIn对象,必须首先创建所有CheckIn对象的数组.您可以通过在NSArray上使用valueForKeyPath方法来完成此 *** 作.然后,您可以使用更简单的谓词搜索该数组.

NSArray *allCheckInObjects = [self.tableInfo valueForKeyPath:@"@distinctUnionOfArrays.cells.object"];nspredicate *namePredicate = [nspredicate predicateWithFormat:@"name contains[c] %@",searchText];NSArray *filteredCheckInObjects = [allCheckInObjects filteredArrayUsingPredicate:namePredicate];

希望这可以帮助!

总结

以上是内存溢出为你收集整理的ios8 – 嵌套对象IOS的NSPredicate问题全部内容,希望文章能够帮你解决ios8 – 嵌套对象IOS的NSPredicate问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1025747.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存