objective-c之在 UITableView 中搜索之NSInvalidArgumentException 错误

sky-heaven 阅读:109 2025-06-02 22:19:02 评论:0

我正在使用“SearchBar 和搜索显示 Controller ”在 UITableView (tblFriends) 中实现搜索栏

这是我的字典 NSarray 过滤的FriendsList(等于friendsList NSarray):

            { 
        gender 
        id 
        name 
        picture  

}

我在 UIViewController 中有表格 View (不在 tableViewController 中),因为表格只占一半 View 。

这是代码:
界面:
#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:]: unrecognized selector sent to instance

我希望能解决这个问题,这是出现此错误的第 6 天。

谢谢你。

请您参考如下方法:

您声明 filteredFriendsList成为 NSMutableArray ,但您分配的是不可变的 NSArray在这里:

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

将其更改为:
filteredFriendsList = [NSMutableArray arrayWithArray:[ClasseSingleton getFriends]]; 


标签:Exception
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号