搜索自定义tableviewCell
我想在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'
尝试的NSLog您的字符串数组。所以你可以明白它是否存在
你没有用Dictionary填充arrayProductNaam。它由NSString填充。现在,在searchTableView函数中,您将这些字符串成员处理为字典,因此也是问题所在。
您试图从字符串中获取keyValue,而不是从字典中获取keyValue。
当我在我的for循环中尝试NSLog时,它不打印任何东西。但就在我的for循环之前,它显示了一些。 – Steaphann 2012-02-02 12:55:26
您是否打印了您的搜索文本? – Hiren 2012-02-02 13:00:26
当我推一个字母时,立即崩溃。但它也打印这封信在我的nslog – Steaphann 2012-02-02 13:11:28