如何在搜索栏文本中显示



我在应用程序中创建了搜索栏。我使用了NSPredicte,但不起作用。这是我的:.m 文件: http://pastebin.com/CMsXpk4N和 .h 文件:

#import <UIKit/UIKit.h>
@interface WordsViewController : UITableViewController <UISearchBarDelegate, UISearchControllerDelegate, UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *finalResultArray;
@property (nonatomic,strong) IBOutlet UISearchBar *searchBar;
@end

我的搜索栏中哪里有错误?

尝试以这种方式更改您的 NSPredicate

NSArray *filterArray= [self.finalResultArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"%K contains[c] %@", @"expression", self.searchBar.text]];

predicateWithFormat将自己在谓词格式字符串中应用 @"expression" key to each element of array, so you don't need to specify self'。

此外,在谓词格式中,键的字符串占位符应为 %K,而不是 %@。

最新更新