UISearchBar不接受多个字符

时间:2013-01-20 14:31:23

标签: ios objective-c uisearchbar

这是当前的视图实现文件:

目前,它使用搜索栏和搜索显示控制器加载单个视图。 这工作得很好。但是,我只能输入一个字符。如果我尝试输入另一个字符,程序会崩溃。

#import "TeamViewController.h"
#import "MatchScoutingViewController.h"

@interface TeamViewController ()
{
    NSArray *teams;
    NSArray *searchResults;
}
@end

@implementation TeamViewController

@synthesize tableContents;
@synthesize sortedKeys;
@synthesize competitionName;
@synthesize rowValue;
bool *canceledSearch = false;

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.sortedKeys objectAtIndex:section];
}

- (IBAction)ViewController:(id)sender;
{
    [self dismissViewControllerAnimated:true completion:nil];
}

- (void)viewDidLoad
{
    NSArray *arrTemp1 = [[NSArray alloc]initWithObjects:@"1885",@"483",@"597",nil];
    NSDictionary *temp =[[NSDictionary alloc]initWithObjectsAndKeys:arrTemp1,@"",nil];
    self.tableContents =temp;
    [temp release];
    self.sortedKeys =[[self.tableContents allKeys] sortedArrayUsingSelector:@selector(compare:)];
    [arrTemp1 release];
    [super viewDidLoad];
}

- (void)dealloc
{
    [tableContents release];
    [sortedKeys release];
    [searchResults release];
    [teams release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark Search Methods

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self handleSearch:searchBar];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    //[self handleSearch:searchBar];
}

- (void)handleSearch:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
    searchBar.text=@"";
    [searchBar resignFirstResponder];
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate
                                predicateWithFormat:@"SELF contains[cd] %@",
                                searchText];

    searchResults = [teams filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                           scope:nil];

    return YES;
}

#pragma mark Table Methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.sortedKeys count];
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
    NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:section]];
    if (table == self.searchDisplayController.searchResultsTableView)
    {
        return [searchResults count];

    }
    else
    {
        return [listData count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

    NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
    }
    else
    {
        NSUInteger row = [indexPath row];
        cell.textLabel.text = [listData objectAtIndex:row];
    }

    teams = listData;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
    NSUInteger row = [indexPath row];
    rowValue = [listData objectAtIndex:row];
    MatchScoutingViewController *second = [[MatchScoutingViewController alloc] initWithNibName:@"MatchScoutingView_iPhone" bundle:nil];
    second.teamName = rowValue;
    second.competitionName = [self competitionName];
    [self presentViewController:second animated:true completion:nil];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end

以下是使用“bt”给我的内容:

#0  0x010ec09b in objc_msgSend ()
#1  0x005b6cb1 in kUIKitUIKitVersionNumber ()
#2  0x000dcf4b in -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] ()
#3  0x000dd01f in -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] ()
#4  0x000c580b in -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] ()
#5  0x000d619b in -[UITableView layoutSubviews] ()
#6  0x0007292d in -[UIView(CALayerDelegate) layoutSublayersOfLayer:] ()
#7  0x010ee6b0 in -[NSObject performSelector:withObject:] ()
#8  0x02299fc0 in -[CALayer layoutSublayers] ()
#9  0x0228e33c in CA::Layer::layout_if_needed ()
#10 0x0228e150 in CA::Layer::layout_and_display_if_needed ()
#11 0x0220c0bc in CA::Context::commit_transaction ()
#12 0x0220d227 in CA::Transaction::commit ()
#13 0x0220d8e2 in CA::Transaction::observer_callback ()
#14 0x01c65afe in __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ ()
#15 0x01c65a3d in __CFRunLoopDoObservers ()
#16 0x01c437c2 in __CFRunLoopRun ()
#17 0x01c42f44 in CFRunLoopRunSpecific ()
#18 0x01c42e1b in CFRunLoopRunInMode ()
#19 0x01bf77e3 in GSEventRunModal ()
#20 0x01bf7668 in GSEventRun ()
#21 0x0002265c in UIApplicationMain ()
#22 0x00001fa2 in main (argc=1, argv=0xbffff218)

我不明白当我尝试输入多个字符时可能导致程序崩溃的原因。有线索吗?

1 个答案:

答案 0 :(得分:0)

单击搜索按钮后,您强制UISearchbar执行resignFirstResponder。为什么?

你的流程:

  • - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  • - (void)handleSearch:(UISearchBar *)searchBar
  • [searchBar resignFirstResponder];

通常带有displayController的searchBar应该是这样的:

  • 您会看到搜索栏。
  • 您输入了一些文字。
  • 应立即根据搜索文本过滤结果。
  • 如果您键入其他字符,则会重新加载与预设搜索文本匹配的结果。

所以你应该实现这个委托方法来过滤结果&更新displayController。

  • 将属性声明为 @property (nonatomic, copy) NSString *searchText;

  • 合成它 @synthesize searchText = _searchText;

  • 像这样实施你的代表:

    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
     if (_searchText != searchText) {
        _searchText = [searchText copy];
    
    NSPredicate *keyPred = [NSPredicate predicateWithBlock:^BOOL(id  obj, NSDictionary *bindings) {
        NSRange range;
            range = [obj rangeOfString:searchText options:NSCaseInsensitiveSearch];
    
        if (range.location != NSNotFound) {
            return YES;
        } else {
            return NO;
        }
    }];
    
    searchResults  = [teams filteredArrayUsingPredicate:keyPred];
    
    // Refresh table view
    [self.searchDisplayController.searchResultsTableView reloadData];
    }
     }    
    

希望能解决你的目的。