Hi,
So, I have a tab bar-based app that is essentially a specialized RSS reader (pulling from preset feeds).
Anyway, I recently decided to incorporate a UISearchBar into one of the app's tabs. I downloaded the source from Apple's TableSearch example, mined it for what I (believe) I need, changed the array names (could this be complicated by the fact that I'm using a mutable array?), and connected the search bar properly in Interface Builder. Everything loads up fine, and the app works just as it should: I can do everything as normal, but when I navigate to the tab with the search--tab X--click within the search bar, and attempt to select a key (say, A), the app freezes immediately, and Xcode tells me that the the simulator is "Terminating due to uncaught exception". How might I fix this? My app certainly didn't do this before.
Here is all of the code relevant to the search bar:
Thanks in advance! I have no idea how to fix this since I'm kind of a noob.
Here's what the debugger says:
So, I have a tab bar-based app that is essentially a specialized RSS reader (pulling from preset feeds).
Anyway, I recently decided to incorporate a UISearchBar into one of the app's tabs. I downloaded the source from Apple's TableSearch example, mined it for what I (believe) I need, changed the array names (could this be complicated by the fact that I'm using a mutable array?), and connected the search bar properly in Interface Builder. Everything loads up fine, and the app works just as it should: I can do everything as normal, but when I navigate to the tab with the search--tab X--click within the search bar, and attempt to select a key (say, A), the app freezes immediately, and Xcode tells me that the the simulator is "Terminating due to uncaught exception". How might I fix this? My app certainly didn't do this before.
Here is all of the code relevant to the search bar:
Code:
@synthesize filteredListContent, savedContent, stories, newsTable, mySearchBar;
- (void)awakeFromNib
{
// create our filtered list that will be the data source of our table, start its content from the master "stories"
filteredListContent = [[NSMutableArray alloc] initWithCapacity: [stories count]];
[filteredListContent addObjectsFromArray: stories];
// this stored the current list in case the user cancels the filtering
savedContent = [[NSMutableArray alloc] initWithCapacity: [stories count]];
// don't get in the way of user typing
mySearchBar.autocorrectionType = UITextAutocorrectionTypeNo;
mySearchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
mySearchBar.showsCancelButton = NO;
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
// only show the status bar's cancel button while in edit mode
mySearchBar.showsCancelButton = YES;
// flush and save the current list content in case the user cancels the search later
[savedContent removeAllObjects];
[savedContent addObjectsFromArray: filteredListContent];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
mySearchBar.showsCancelButton = NO;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[filteredListContent removeAllObjects]; // clear the filtered array first
// search the table content for cell titles that match "searchText"
// if found add to the mutable array and force the table to reload
//
NSString *cellTitle;
for (cellTitle in stories)
{
NSComparisonResult result = [cellTitle compare:searchText options:NSCaseInsensitiveSearch
range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
[filteredListContent addObject:cellTitle];
}
}
[newsTable reloadData];
}
// called when cancel button pressed
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
// if a valid search was entered but the user wanted to cancel, bring back the saved list content
if (searchBar.text.length > 0)
{
[filteredListContent removeAllObjects];
[filteredListContent addObjectsFromArray: savedContent];
}
[newsTable reloadData];
[searchBar resignFirstResponder];
searchBar.text = @"";
}
// called when Search (in our case "Done") button pressed
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
Code:
- (void)dealloc {
[newsTable release];
[mySearchBar release];
[stories release];
[filteredListContent release];
[savedContent release];
[super dealloc];
}
Thanks in advance! I have no idea how to fix this since I'm kind of a noob.
Here's what the debugger says: