1

我在試圖弄清楚如何在UItableview上顯示過濾後的搜索結果時遇到了很多麻煩。在UItableview中搜索 - 使用嵌套數組NSpredicate

我的顯示器的方法工作得很好......但是當我嘗試尋找我的陣列中的具體數據,只顯示FIRST OBJECT OF THE SECTIONS或與此錯誤消息崩潰:

*終止應用程序由於未捕獲的異常'NSRangeException',原因: ' - [__ NSArrayI objectAtIndex:]:索引2超出範圍[0 .. 0]' *

我的陣列結構:AllDataGroupbyServiceArray

(
    { 
    Companies =   (
        { 
      AddressInformation = h3k2w3; 
      Name = Teg; 
      Service = ""; 
     } 
    ); 
    Services = ""; 
}, 
    { 
    Companies =   (
        { 
      AddressInformation = j9g2k6; 
      Name = Megan; 
      Service = Electrician; 
     }, 
        { 
      AddressInformation = h2k2w3; 
      Name = test; 
      Service = Electrician; 
     }, 
        { 
      AddressInformation = h2t3b3; 
      Name = Janet; 
      Service = Electrician; 
     }, 
        { 
      AddressInformation = "J4J 1H7"; 
      Name = Rick; 
      Service = Electrician; 
     }, 
        { 
      AddressInformation = "H1T 4B6"; 
      Name = Herber; 
      Service = Electrician; 
     }, 
        { 
      AddressInformation = "J0K 3B0"; 
      Name = test; 
      Service = Electrician; 
     }, 
        { 
      AddressInformation = h2k2w3; 
      Name = test; 
      Service = Electrician; 
     }, 
        { 
      AddressInformation = h2k2w3; 
      Name = canada; 
      Service = Electrician; 
     } 
    ); 
    Services = Electrician; 
}, 
    { 
    Companies =   (
        { 
      AddressInformation = J0L2K0; 
      Name = "Colas Bn"; 
      Service = Mason; 
     }, 
        { 
      AddressInformation = J5A1M2; 
      Name = mrnoskill2; 
      Service = Mason; 
     }, 
        { 
      AddressInformation = J0L2K0; 
      Name = test8; 
      Service = Mason; 
     }, 
        { 
      AddressInformation = J5C1S3; 
      Name = test6; 
      Service = Mason; 
     }, 
        { 
      AddressInformation = h3r1z2; 
      Name = Jason; 
      Service = Mason; 
     }, 
        { 
      AddressInformation = j7h2k7; 
      Name = max; 
      Service = Mason; 
     }, 
        { 
      AddressInformation = J5Z2W5; 
      Name = Fred; 
      Service = Mason; 
     }, 
        { 
      AddressInformation = "H4G 1K6"; 
      Name = Joe; 
      Service = Mason; 
     }, 
        { 
      AddressInformation = j3y7a6; 
      Name = Tets; 
      Service = Mason; 
     } 
    ); 
    Services = Mason; 
}, 
    { 
    Companies =   (
        { 
      AddressInformation = h2k2w3; 
      Name = jake; 
      Service = Plumber; 
     } 
    ); 
    Services = Plumber; 
} 
) 

這裏的濾波方法:

-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope { 
// Update the filtered array based on the search text and scope. 
// Remove all objects from the filtered search array 
[filteredObject removeAllObjects]; 
// Filter the array using NSPredicate 


NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY %K.%K CONTAINS[cd] %@", 
          @"Companies",@"Name",searchText]; 


filteredObject = [NSMutableArray arrayWithArray:[AllDataGroupbyServiceArray filteredArrayUsingPredicate:predicate]]; 
} 

在部分代碼行數:效果很好,但沒有與搜索

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

#warning Incomplete method implementation. 

// Returns the number of items in the array associated with the letter for this section. 



// Check to see whether the normal table or search results table is being displayed and return the count from the appropriate array 

if (tableView == self.searchDisplayController.searchResultsTableView) { 

    NSArray *filterCount = [filteredObject valueForKey:@"Services" ]; 

    return [filterCount count]; 

} else { 


NSString* serviceSection = [publicServiceArray objectAtIndex:section]; 
NSArray* arrayservicescount = (NSArray*)[ServicesDictionary objectForKey:serviceSection]; 
    return arrayservicescount.count; 


} 

} 

表頭:效果很好

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 



    // Condition for the search tableview 
     if(tableView == self.searchDisplayController.searchResultsTableView) 




{ 
    NSArray *sections = [NSArray array] ; 

     sections = [ filteredObject valueForKey:@"Services"] ; 

     return [ sections objectAtIndex:section] ; 


    } 




NSArray *sections = [NSArray array] ; 

    sections = [[myObject valueForKeyPath:@"@distinctUnionOfObjects.Service"]sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] ; 

    return [sections objectAtIndex:section]; 



} 

Cel l顯示問題:不會爲搜索

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
    cell=[[UITableViewCell alloc]initWithStyle: 
      UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
} 


NSMutableDictionary *tmpDict = [myObject objectAtIndex:AllDataGroupbyServiceArray.count]; 

// set the search object from the appropriate array 

// Condition for the search tableview 
if (tableView == self.searchDisplayController.searchResultsTableView) { 
    tmpDict = [[filteredObject valueForKey:@"Companies" ]objectAtIndex:indexPath.row] 


} else { 

    tmpDict = [[[AllDataGroupbyServiceArray valueForKey:@"Companies" ] objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] ; 





} 




NSMutableString *text; 

text = [tmpDict valueForKey:name] ; 


NSMutableString *detail; 
detail =[NSMutableString stringWithFormat:@"Postal code : %@ ", [tmpDict valueForKey:serviceAddress]]; 

// NSMutableString *images; 
    // images = [NSMutableString stringWithFormat:@"%@ ", 
    //   [tmpDict objectForKey:serviceAddress]]; 
    // NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:thumbnail]]; 
    // NSData *data = [NSData dataWithContentsOfURL:url]; 
    // UIImage *img = [[UIImage alloc]initWithData:data]; 



cell.textLabel.text = text ; 
cell.detailTextLabel.text= detail ; 
cell.imageView.frame = CGRectMake(0, 0, 80, 70); 


// cell.imageView.image =img; 

return cell; 


} 

我知道我非常接近的答案工作,但任何幫助,將不勝感激提前

感謝

回答

0

這似乎是一個在這條線的問題

你已經有一個filteredObject這一行

filteredObject = [NSMutableArray arrayWithArray:[AllDataGroupbyServiceArray filteredArrayUsingPredicate:predicate]]; 

再次要篩選這個數組服務鍵

if (tableView == self.searchDisplayController.searchResultsTableView) { 

NSArray *filterCount = [filteredObject valueForKey:@"Services" ]; 

*您應該返回filteredObject數*

return [filterCount count]; 

} 
+0

我改變TE陣列[filteredObject計數現在它給了我下面的錯誤: - [__ NSArrayI長度]:無法識別的選擇器發送到實例0x9c85520 ***由於未捕獲的異常'NSInvalidArgumentException',原因:' - [__ NSArrayI長度]:無法識別的選擇器發送到實例0x9c85520' – user3320087