2012-04-02 102 views
1

我執行在一個UITableView(tblFriends)搜索欄有「搜索欄和搜索顯示控制器」搜索 - NSInvalidArgumentException錯誤

這是我的字典的NSArray filteredFriendsList(等於friendsList的NSArray):

  { 
     gender 
     id 
     name 
     picture 

}

我有一個UIViewController表視圖,(不是在tableViewController),因爲該表只佔用半個視圖。

這是代碼: 接口:

#import <UIKit/UIKit.h> 
#import "ClasseSingleton.h" 
#import "FBConnect.h" 

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 
{ 

    NSArray *friendsList; 
    NSDictionary *friendsDict; 

    NSMutableArray *filteredFriendsList; 

    IBOutlet UITableView *tblFriends; 

} 

@property (nonatomic, retain) NSArray *friendsList; 
@property (nonatomic, retain) NSMutableArray *filteredFriendsList; 

-(void)getFriends; 


@end 

實現

#import "ViewController.h" 

@implementation ViewController 

@synthesize friendsList, filteredFriendsList; 

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

    [tblFriends setDelegate:self]; 
    [tblFriends setDataSource:self]; 


} 

- (void)getFriends{ 

    NSLog(@"ENTRATO - getFriends"); 

    //Copy ARRAY in other ARRAY 
    friendsList = [NSArray arrayWithArray:[ClasseSingleton getFriends]]; 

    filteredFriendsList = [NSArray arrayWithArray:[ClasseSingleton getFriends]]; 


    NSLog(@"getFriends : DESCRIPTION\n\n %@", [friendsList description]); 
    NSLog(@"Count friendslist: %i", [friendsList count]); 

    [tblFriends reloadData]; 

} 


//     *****TABLE MANAGEMENT*****     // 


//Nuber of cells 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

    NSLog(@"tabella1"); 
    return [filteredFriendsList count]; 


} 

//Populate the table 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPat{ 

    static NSString * cellIdentifier = @"cell"; 

    //Set Style cell 
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil){ 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 

    } 

    friendsDict = [filteredFriendsList objectAtIndex:indexPat.row]; 


    //Set CELL TEXT 
    NSString *cellValue = [friendsDict objectForKey:@"name"]; 

    NSLog(@"TBL %@", cellValue); 
    [cell.textLabel setText:cellValue]; 
    return cell; 

} 

//       SEARCH IN TABLE 

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{ 
    [self filterContentForSearchText:searchString scope: 
    [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; 

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

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption{ 

    [self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope: 
    [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]]; 

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

- (void)searchBarCancelButtonClicked:(UISearchBar *)saearchBar { 
    [self.filteredFriendsList removeAllObjects]; 
    [self.filteredFriendsList addObjectsFromArray: friendsList]; 
} 


- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{ 

    /* 
    Update the filtered array based on the search text and scope. 
    */ 
    [self.filteredFriendsList removeAllObjects]; // First clear the filtered array. 

    /* 
    Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array. 
    */ 
    NSString *cellTitle; 
    for (cellTitle in friendsList){ 
    // for (cellTitle in [friendsDict objectForKey:@"name"]){  
     NSComparisonResult result = [cellTitle compare:searchText options:NSCaseInsensitiveSearch range:NSMakeRange(0, [searchText length])]; 
     if (result == NSOrderedSame){ 
      [filteredFriendsList addObject:cellTitle]; 
     } 
    } 
} 

... 

@end 

每次我把搜索一些字符酒吧與此錯誤的應用程序崩潰:

'NSInvalidArgumentException' 的,原因:' - [__ NSCFDictionary compare:options:range:]:無法識別的選擇器發送到實例

我希望能解決這個問題,這是第6天出現這個錯誤。

謝謝。

回答

4

您聲明filteredFriendsList是一個NSMutableArray,但你在這裏分配的immuatble NSArray它:

filteredFriendsList = [NSArray arrayWithArray:[ClasseSingleton getFriends]]; 

它改成這樣:

filteredFriendsList = [NSMutableArray arrayWithArray:[ClasseSingleton getFriends]]; 
+0

或'[ClasseSingleton getFriends] mutableCopy ]' – warrenm 2012-04-02 17:59:03

+0

:(現在有一個新的錯誤:'NSInvalidArgumentException',原因:' - [__ NSCFDictionary compare:options:range:]: – Byteros 2012-04-02 19:44:46

+0

這不是整個錯誤 – yuji 2012-04-02 19:53:38