我目前正试图按照本教程进行搜索。 http://www.appcoda.com/how-to-add-search-bar-uitableview/ 但是,我在我的应用程序崩溃,但我不知道,如何解决它的点。在TableView上执行搜索后,应用程序崩溃
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 pa rameter超出界限。我真的不明白指数(1)如何超越界限(1)。我知道,我只有零个或一个返回行(取决于搜索短语)。
我刚开始今天的工作搜索,所以我没有任何线索,什么那些棘手的斑点。任何建议表示赞赏:)谢谢,盐池
编辑:好了,所以我发现我的地方问题可能是......我紧跟着这个问题的顶部提到的教程..这种方法应该返回数组与搜索结果。
- (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,而我只有一个数据在我的搜索结果数组)
之后,我改变了我的NSLog显示数组的内容...插入匹配的字母后,相同的项目被添加到我的数组(所以我有另一个项目在我的数组中,有相同的数据作为我的阵列中的第一项)
编辑2:好吧,所以我的问题是,每当我从字典保存json数据到数组(以便我可以使用数组进行筛选结果)我在cellForRowAtIndexPath中执行它..所以我的数组每次都获得相同的值,搜索表是“重新填充的”。现在我需要找到一些方法只有在数据不同的情况下才将数据加载到数组中。
好,指标(1)超出范围(1),因为一个NSArray在它只有一个元素应该只有在索引的元素(0 )。所以发生的是,当只有一个元素(在索引0处)时,表视图询问其数据源为/ second /元素(索引1)。如果表格视图不知道只有一行,就不应该这样做。你可以在你的程序的某个地方设置一个断点,看看你的tableview返回的是什么 - [UITableView numberOfSections]和 - [UITableView numberOfRowsInSection:],看看它们是否是你期望的值? – eyebrowsoffire
我的问题是,我不确定在哪里设置断点....在我有机会真正将某些内容放入searchBar之前会达到断点(这是因为我的tableview与所有项目都会事先加载。 nslog这些计数,但没有任何显示在控制台 – Yanchi
我想我接近解决这个问题...我 - (无效)filterContentForSearchText:(NSString *)searchText作用域:(NSString *)作用域填充我的数组与更多的索引我添加字母到searchBar – Yanchi