2012-11-10 58 views
0

我在UITableView上實現UISearchBarDelegate時出現了問題,其中每次嘗試搜索UITableView內部的某些內容時,無論是顯示期望的結果還是崩潰應用程序與一個錯誤說:UITableView上的UISearchBarDelegate與部分問題

終止應用程序由於未捕獲的異常 'NSRangeException',原因: '* - [__ NSArrayI objectAtIndex:]:索引1超出範圍[0 .. 0]'

這裏是我的頭文件:

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate> 
{ 
    UITableView *mainTableView; 

    NSMutableArray *contentsList; 
    NSMutableArray *searchResults; 
    NSString *savedSearchTerm; 

    NSMutableArray *ivSectionKeys; 
    NSMutableDictionary *ivSectionContents; 
} 

@property (nonatomic, retain) IBOutlet UITableView *mainTableView; 
@property (nonatomic, retain) NSMutableArray *contentsList; 
@property (nonatomic, retain) NSMutableArray *searchResults; 
@property (nonatomic, copy) NSString *savedSearchTerm; 

@property (nonatomic, retain) NSMutableArray *sectionKeys; 
@property (nonatomic, retain) NSMutableDictionary *sectionContents; 

- (void)handleSearchForTerm:(NSString *)searchTerm; 

@end 

,而這是在我的實現文件:

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

@synthesize mainTableView; 
@synthesize contentsList; 
@synthesize searchResults; 
@synthesize savedSearchTerm; 

@synthesize sectionKeys = ivSectionKeys; 
@synthesize sectionContents = ivSectionContents; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    NSMutableArray *keys = [[NSMutableArray alloc] init]; 
    NSMutableDictionary *contents = [[NSMutableDictionary alloc] init]; 

    NSString *colorKey = @"Colors"; 
    NSString *clothingKey = @"Clothing"; 
    NSString *miscKey = @"Misc"; 

    [contents setObject:[NSArray arrayWithObjects:@"Red", @"Blue", nil] forKey:colorKey]; 
    [contents setObject:[NSArray arrayWithObjects:@"Pants", @"Shirt", @"Socks", nil] forKey:clothingKey]; 
    [contents setObject:[NSArray arrayWithObjects:@"Wankle Rotary Engine", nil] forKey:miscKey]; 

    [keys addObject:clothingKey]; 
    [keys addObject:miscKey]; 
    [keys addObject:colorKey]; 

    self.sectionKeys = keys; 
    self.sectionContents = contents; 

    // Restore search term 
    if (self.savedSearchTerm) 
    { 
     self.searchDisplayController.searchBar.text = self.savedSearchTerm; 
    } 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 

    // Save the state of the search UI so that it can be restored if the view is re-created. 
    self.savedSearchTerm = self.searchDisplayController.searchBar.text; 

    self.searchResults = nil; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    [self.mainTableView reloadData]; 
} 

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

- (void)handleSearchForTerm:(NSString *)searchTerm 
{ 
    self.savedSearchTerm = searchTerm; 

    if (self.searchResults == nil) 
    { 
     NSMutableArray *array = [[NSMutableArray alloc] init]; 
     self.searchResults = array; 
     array = nil; 
    } 

    [self.searchResults removeAllObjects]; 

    if ([self.savedSearchTerm length] != 0) 
    { 
     for (NSString *currentString in self.sectionContents) 
     { 
      if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound) 
      { 
       [self.searchResults addObject:currentString]; 
      } 
     } 
    } 
} 

#pragma mark - 
#pragma mark UITableViewDataSource Methods 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    NSInteger sections = [self.sectionKeys count]; 

    return sections; 
} 

- (NSString *)tableView:(UITableView *)tableView 
titleForHeaderInSection:(NSInteger)section 
{ 
    NSString *key = [self.sectionKeys objectAtIndex:section]; 

    return key; 
} 

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section 
{ 
    NSString *key = [self.sectionKeys objectAtIndex:section]; 
    NSArray *contents = [self.sectionContents objectForKey:key]; 

    NSInteger rows; 

    if (tableView == [[self searchDisplayController] searchResultsTableView]) 
     rows = [self.searchResults count]; 
    else 
     rows = [contents count]; 

    NSLog(@"rows is: %d", rows); 

    return rows; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *key = [self.sectionKeys objectAtIndex:indexPath.section]; 
    NSArray *contents = [self.sectionContents objectForKey:key]; 
    NSString *contentForThisRow = [contents objectAtIndex:indexPath.row]; 

    if (tableView == [self.searchDisplayController searchResultsTableView]) 
     contentForThisRow = [self.searchResults objectAtIndex:indexPath.row]; 
    else 
     contentForThisRow = [contents objectAtIndex:indexPath.row]; 

    static NSString *CellIdentifier = @"CellIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    cell.textLabel.text = contentForThisRow; 

    return cell; 
} 

#pragma mark - 
#pragma mark UITableViewDelegate Methods 

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

#pragma mark - 
#pragma mark UISearchDisplayController Delegate Methods 

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

    // Return YES to cause the search result table view to be reloaded. 

    return YES; 
} 

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller 
{ 
    self.savedSearchTerm = nil; 

    [self.mainTableView reloadData]; 
} 

@end 

我想我的問題是在我的for循環,但我真的不知道。請幫忙。謝謝!

+0

是款數字正確,即使你過濾行數組? sectionKeys/sectionContents也被訪問。 –

+0

是的。我只想要搜索sectionContents。 – jaytrixz

回答

0

你需要更新你所有的表視圖的數據源和委託方法處理兩個表(你只能這樣做在某些):

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    NSInteger sections = [self.sectionKeys count]; 

    return sections; 
} 

應該是:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    if (tableView == self.tableView) { 
     NSInteger sections = [self.sectionKeys count]; 

     return sections; 
    } else { // assume it's the search table 
     // replace this with the actual result 
     return numberOfSectionsForSearchTable; 
    } 
} 
+0

我試過這樣做:if(tableView == [[self searchDisplayController] searchResultsTableView]){ NSInteger sections = [self.sectionContents count]; 返回段; }否則{ NSInteger的部分= [self.sectionKeys計數]; 返回段; }但它仍然返回相同的錯誤。 – jaytrixz

+0

好的,發生了什麼事?你更新了其餘的方法嗎? – rmaddy

+0

我只更新了numberOfSectionsInTableView方法。我還需要更新哪些方法? – jaytrixz