2012-02-02 141 views
0

我想在tableview中進行搜索。但在這個表格視圖中,我使用了一個自定義tableview Cell。所以對於數據部分。我在視圖中選擇一個分類。然後在下一個視圖中,我獲得了這個分類中的所有產品。 我跟着這個tutorial實現了所有的方法。搜索自定義tableviewCell

這是我獲取產品的功能。

-(void) fillArrayProducts:(NSString *)cat{ 
    NSMutableString *postString = [NSMutableString stringWithString:kGETProducts]; 
    [postString appendString:[NSString stringWithFormat:@"?%@=%@",@"Pro_cat",cat]]; 
    [postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

    NSMutableURLRequest *request= [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]]; 
    [request setHTTPMethod:@"POST"]; 

    postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; 

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:postString]]; 

    NSError *error; 

    json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

    arrayProducts = [[NSMutableArray alloc] init]; 
    copyArrayProducts = [[NSMutableArray alloc] init]; 
    arrayProductNaam = [[NSMutableArray alloc] init]; 
    for (int i=0; i<[json count]; i++) { 
     arrayProduct = [[NSMutableArray alloc] init]; 
     [arrayProduct addObject:[json objectAtIndex:i]]; 

     [arrayProducts addObject:arrayProduct]; 
     [arrayProductNaam addObject:[[[arrayProducts valueForKey:@"Pro_naam"] objectAtIndex:i]objectAtIndex:0]]; 
     [copyArrayProducts addObject:arrayProduct]; 
    } 
    NSDictionary *productsDict = [NSDictionary dictionaryWithObject:arrayProductNaam forKey:@"Products"]; 
    NSLog(@"%@",arrayProductNaam); 

} 

沒有我在做什麼。我在這個數組中有一個數組'arrayProducts',我從'arrayProduct'放了數組。在arrayProduct中,您可以找到Pro_id,Pro_naam,Pro_prijs。但我只想搜索Pro_naam。所以我也只填寫了一個數組(arrayProductNaam)和產品名稱。

所以這裏是我做我的搜索的部分。

- (void) searchTableView { 

    NSString *searchText = searchbar.text; 
    NSMutableArray *searchArray = [[NSMutableArray alloc] init]; 

    for (NSDictionary *dictionary in arrayProductNaam) 
    { 
     NSArray *array = [dictionary objectForKey:@"Pro_naam"]; 
     [searchArray addObjectsFromArray:array]; 
    } 

    for (NSString *sTemp in searchArray) 
    { 
     NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch]; 

     if (titleResultsRange.length > 0) 
      [copyArrayProducts addObject:sTemp]; 
    } 


} 

我認爲這是我得到這個錯誤。

2012-02-02 13:22:40.958 MamzelBestelling2 [23727:F803] - [__ NSCFString objectForKey:]:無法識別的選擇發送到實例0x6c34780 2012-02-02 13:22:40.959 MamzelBestelling2 [23727 :由於未捕獲的異常 'NSInvalidArgumentException' F803] *終止 應用程序,原因是: ' - [__ NSCFString objectForKey:]:無法識別的選擇發送到實例 0x6c34780'

回答

0

嘗試的NSLog您的字符串數組。所以你可以明白它是否存在

+0

當我在我的for循環中嘗試NSLog時,它不打印任何東西。但就在我的for循環之前,它顯示了一些。 – Steaphann 2012-02-02 12:55:26

+0

您是否打印了您的搜索文本? – Hiren 2012-02-02 13:00:26

+0

當我推一個字母時,立即崩潰。但它也打印這封信在我的nslog – Steaphann 2012-02-02 13:11:28

1

你沒有用Dictionary填充arrayProductNaam。它由NSString填充。現在,在searchTableView函數中,您將這些字符串成員處理爲字典,因此也是問題所在。

0

您試圖從字符串中獲取keyValue,而不是從字典中獲取keyValue。