1

Here is the current views implementation file:

Currently it loads a single view with a Search Bar and Search Display Controller. This was working just fine. However, I can only type in one character. If I try to type in another character, the program crashes.

#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

Here is what using "bt" gives me:

#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)

I don't understand what could be causing the program to crash when I try to enter multiple characters. Any clues?

4

1 回答 1

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];
    }
     }    
    

希望这能解决你的目的。

于 2014-02-17T09:39:56.710 回答