我目前正在尝试按照本教程来实现搜索。 http://www.appcoda.com/how-to-add-search-bar-uitableview/ 但是,我的应用程序崩溃了,但我不知道如何修复它。
我的应用程序中的 JSON 目前只有一行的数据(我无法更改它,因为我没有数据库访问权限)。
我最初的看法看起来不错。TableView 已填充(有 1 行)。当我点击搜索栏并输入一些不在该项目名称中的字符时,我会看到正常的“未找到结果”屏幕。当我输入项目名称中存在的任何字符时,它会按预期显示过滤的项目。但是,当我输入第二个字母(即项目名称)时,我的应用程序崩溃,并在控制台中给出以下输出。
Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (1)'
现在到我的代码。numberOfRowsInSection 方法根据搜索进行修改。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [[resultSearch valueForKeyPath:@"data.auctions"]count];
}
}
numberOfSectionsInTableView 只有一个部分硬编码(返回 1;因为我目前不需要更多)
现在我的 cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; //custom cell
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSDictionary *aucdict = [jsonAukResultsSearch objectAtIndex:indexPath.row]; //assigning current row from NSMutableArray(jsonAukResultsSearch) to Dictionary, this is probably crash spot
NSURL *imageURL = [NSURL URLWithString:[aucdict objectForKey:@"img_url"]];
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//sending request to get picture
UIImage *imageLoad = [[UIImage alloc] initWithData:result];
NSString *timeString = [aucdict objectForKey:@"time_left"];
NSString *priceString = [aucdict objectForKey:@"current_value"];
// Configure the cell...
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
} else {
cell.nameLabel.text = [aucdict objectForKey:@"name"];
}
//cell.thumbnailImageView.image = imageLoad;
//commented while crashes are happening
return cell;
}
现在,在我的应用程序崩溃后,我的线路与
NSDictionary *aucdict = [jsonAukResultsSearch objectAtIndex:indexPath.row];
是荧光笔,所以我猜,这可能是 indexPath 参数超出范围的地方。我真的不明白 index(1) 怎么会越界(1)。我知道,我只有零个或一个返回行(取决于搜索短语)。
我今天才开始搜索,所以我不知道那些棘手的地方是什么..任何建议都值得赞赏:)谢谢你,Yanchi
编辑:好的,所以我发现我的问题可能出在哪里......我密切关注这个问题顶部提到的教程。这个方法应该返回带有搜索结果的数组。
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
searchResults = [poleMien filteredArrayUsingPredicate:resultPredicate];
NSLog(@"%u",[searchResults count]);
}
如您所见,我添加了 NSLog... 现在,当我添加匹配的第一个字母时,控制台输出为 1(应该是),因此我的过滤结果将毫无问题地显示。但是,当我输入另一个匹配的字母时,我的 searchResults 数组将增加到 2(好像它会计算“匹配”字母),这可能是我崩溃的原因(尝试访问索引 1,而我只有一个的数据我的 searchResults 数组中的项目)
之后我更改了我的 NSLog 以显示数组的内容......在插入匹配的字母后,相同的项目被添加到我的数组中(所以我的数组中有另一个项目,它与我的数组中的第一项具有相同的数据)
编辑 2:好的,所以我的问题是,每当我将 json 数据从字典保存到数组(以便我可以使用数组来过滤结果)时,我都会在 cellForRowAtIndexPath 中执行此操作。所以我的数组每次都加载相同的值,搜索表被“重新填充”。现在我需要找到一些方法来将数据加载到数组中,前提是它是不同的项目。