2014-12-05 3 views
4

"self.tableInfo"(NSMutableArray)에 NSPredicate을 사용하면 CheckIn 객체를 얻기 위해 다음과 같은 구조의 조건부 키 "이름"을 사용할 수 있습니다. self.tableInfo에는 SectionInfo의 배열이 있고 각 SectionInfo에는 셀 (CellInfo)이 있으며 각 셀에는 checkin 객체가 있습니다. 이것은 항상 0 카운트 저를 반환중첩 된 객체에 대한 NSPredicate 문제 IOS

- (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]; 
} 

:

- (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); 
} 

내가 사용하고있는 방법은 같은 것입니다. 이 문제를 해결할 더 좋은 아이디어가 있습니까?

답변

1

이 술어에 몇 가지 문제가있는 것 같습니다. 우선, 당신의 술어에서 "CellInfo.object"SELF.cells.object contains[c] %@ 대신 "CellInfo.object.name"대신 검색하고 있습니다.

두 번째로 배열 내의 배열 (tableInfo 배열의 SectionInfo.cells)을 검색 할 때 단순히 점 표기법을 사용하여 내부 배열을 검색 할 수 없습니다. 배열에서 원하는 항목과 일치하는 항목을 지정해야합니다. 키워드 ANYALL 또는 NONE 중 하나를 사용하여이 작업을 수행 할 수 있습니다.

예를 들어, CheckIn.name는이 작업을 수행 할 것입니다 검색 텍스트가 포함 된 CellInfo이있는 SectionInfo를 얻을 : 그러나,이 SectionInfo의 대신 같은 체크인 객체의 배열을 반환

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

너는 원했어. 대신 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]; 

희망이 있습니다.

+0

감사합니다. NSPredicate * namePredicate = [NSPredicate predicateWithFormat : @ "모든 셀. 객체 이름에 [c] % @", searchText]가 포함되어 있습니다. NSArray * filteredSections = [self.tableInfo filteredArrayUsingPredicate : namePredicate]; 그러나 결과는 항상 빈 배열입니다. sectionInfo 객체가 필요합니다. –

+0

검색하기 전에 self.tableInfo가 초기화되고 채워 졌는지 확신합니까? 나는 NSLog() 배열을 찾고 당신이 찾고있는 객체를 찾을 수 있는지를 확인하기 위해 단지 술어가 실패한 술어라는 것을 확신해야한다. –

+0

예 콘솔에서 "Po self.tableInfo"를 사용하는 동안 NSLog()를 사용하여 로그를 다시 확인했습니다. NSLog (@ "self.tableInfo before predicate : % @", self.tableInfo); 술어 이전에 self.tableInfo :( "" ) NSLog (@ "filteredSections : % @", filteredSections); filteredSections :( ) NSLog (@ "filteredSections 배열 수 : % lu", (부호없는 길이) [filteredSections count]); filteredSections 배열 개수 : 0 검색 키 "a"가 내 Checkin 개체에 있음을 확신합니다. –

0

다음 코드로 해결합니다. 나는 "최근"및 "기타"배열을 하나의 배열에 가지고 각 배열에 술어를 적용합니다. 검색 기준은 "from_user"NSDictionary에서 "name"이었습니다.

-(void)filterContentForSearchText:(NSString *)searchText 
{ 
    [self.filterResultArray removeAllObjects]; 
    NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"(from_user.name contains[cd] %@)", searchText]; 

    NSMutableArray *recentSearch=[[NSMutableArray alloc] init]; 
    //Recent 
    if ([[[self connectioUserArray] objectAtIndex:1] count]==0) { 
     //Recent is blank 
    } 
    else{ 
     //else 
     recentSearch=[[[[self connectioUserArray] objectAtIndex:0] filteredArrayUsingPredicate:namePredicate] mutableCopy]; 
    } 

    //other 
    self.filterResultArray = [[[[self connectioUserArray] objectAtIndex:1] filteredArrayUsingPredicate:namePredicate] mutableCopy]; 

    if ([recentSearch count]==0) { 
     //no records 
    } 
    else{ 
     //if recent records 
     for (NSMutableDictionary *recentConnectoin in recentSearch) { 
      [self.filterResultArray addObject:recentConnectoin]; 
     } 
    } 

    //Reload table view 
    [self.tableView reloadData]; 

} 
관련 문제