Issues adding search to iPhone app
Posted
by Graeme
on Stack Overflow
See other posts from Stack Overflow
or by Graeme
Published on 2010-03-22T09:08:03Z
Indexed on
2010/03/22
9:11 UTC
Read the original article
Hit count: 310
Hi,
Basically I'm trying to add a search function to my iPhone app, but I'm not having much luck at the moment. I've downloaded the Apple provided Table Search app, and have copied across the code to mine. It builds OK, but here's the problem. Unlike the Apple example, all my data is stored in an array, that is accessed by calling [ciParser.currentArray].
Any ideas on how to change the code to suit my needs? I'm getting an error "Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'" whenever I click on the search bar and the app exits. Below is the code in particular that the debugger highlights as being troublesome.
Apparently this error means the database trying to be searched is empty, but I could be wrong.
FYI my app downloads and parsers an RSS feed using a class which is referenced by ciParser - which in turn stores the downloaded content in an array that I need to search.
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
/*
If the requesting table view is the search display controller's table view, configure the cell using the filtered content, otherwise use the main list.
*/
CurrentItem * nextCurrentItem=[ciParser.currentArray objectAtIndex:indexPath.row];
if (tableView == self.searchDisplayController.searchResultsTableView)
{
(Code which debugger points to as being wrong) nextCurrentItem = [self.filteredListContent objectAtIndex:indexPath.row]; } else { nextCurrentItem = [ciParser.currentArray objectAtIndex:indexPath.row]; }
NSString*settingValue = [[NSUserDefaults standardUserDefaults]stringForKey:@"state"];
if ([settingValue isEqualToString:@"New South Wales"]) {
cell.textLabel.text=nextCurrentItem.title;
}
else if ([settingValue isEqualToString:@"Western Australia"]) {
cell.textLabel.text=@"The FESA does not provide any Current Incident reports.";
}
else if ([settingValue isEqualToString:@"Victoria"]) {
cell.textLabel.text=nextCurrentItem.title;
}
else if ([settingValue isEqualToString:@"South Australia"]) {
cell.textLabel.text=nextCurrentItem.title;
}
else if ([settingValue isEqualToString:@"Tasmania"]) {
cell.textLabel.text=nextCurrentItem.title;
}
// Set up the cell...
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
© Stack Overflow or respective owner