这是我的问题,我似乎无法在搜索栏中输入2个字符,而且应用程序崩溃了。第一个应该工作,但当我搜索除第一个CRASH之外的任何其他蝴蝶!请帮忙! [filteredComList removeAllObjects]; [filteredImgList removeAllObjects]; [filteredSciList removeAllObjects];
[filteredComList addObjectsFromArray:buttComNameList];
[filteredSciList addObjectsFromArray:buttSciNameList];
[filteredImgList addObjectsFromArray:butterflyImages];
}else {
[filteredComList removeAllObjects];
[filteredImgList removeAllObjects];
[filteredSciList removeAllObjects];
for (NSString *string in buttComNameList) {
NSRange range = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
[filteredComList addObject:string];
}
}
for (NSString *string in buttSciNameList) {
NSRange range = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
[filteredSciList addObject:string];
}
}
for (NSString *string in butterflyImages) {
NSRange range = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
[filteredImgList addObject:string];
}
}
}
[myButterflyTable reloadData];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
_searchWasActive = NO;
[butterflySearchBar resignFirstResponder];
[self.tableView setFrame:self.view.bounds];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
_searchWasActive = YES;
NSLog(@"began editing");
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
_searchWasActive = NO;
NSLog(@"ended editing");
}
下面的表委托方法
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section
return [filteredComList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
Defenitions *def = [[Defenitions alloc]init];
// Configure the cell...
//[self fetchedResultsController:[self fetchedResultsControllerForTableView:tableView] configureCell:cell atIndexPath:indexPath];
if (_searchWasActive) {
[def defineButterfly:[filteredComList objectAtIndex:indexPath.row] defineLatin: [filteredSciList objectAtIndex:indexPath.row] setImage:[filteredImgList objectAtIndex:indexPath.row]];
}else {
[def defineButterfly:[buttComNameList objectAtIndex:indexPath.row] defineLatin:[buttSciNameList objectAtIndex:indexPath.row] setImage:[butterflyImages objectAtIndex:indexPath.row]];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[cell.contentView addSubview:[def commonName]];
[cell.contentView addSubview:[def latinName]];
[cell.contentView addSubview:[def butterflyImage]];
return cell;
}