0
我想在我的代碼中使用NSPredicate搜索名稱。搜索有效,但不會返回適當的結果。當我搜索一個名稱,例如「Colin」時,它將返回表中的所有其他名稱或另一個名稱,例如「Mike」,但如果輸入一個不存在的隨機字符串,它會返回:「未找到結果」。當我鍵入搜索欄(如麗莎)的名稱,我希望它找到的域名(麗莎),並返回它,但它沒有做到這一點NSPredicate不能返回正確的結果
這是我的代碼:
- (void)filterData {
[self.filteredSearch removeAllObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self CONTAINS [cd] %@", mySearchBar.text];
self.filteredSearch = [[self.name filteredArrayUsingPredicate:predicate] mutableCopy];
}
self.name返回表中的所有名稱,並從屬性列表中獲取該名稱
NSString *path = [[NSBundle mainBundle] pathForResource:@"lecturers" ofType:@"plist"];
lecturers= [[NSDictionary alloc] initWithContentsOfFile:path];
// Allocate key objects from lecturers list
name = [lecturers objectForKey:@"name"];
- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
[self filterData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (tableView == self.tableView) {
return self.name.count;
}
[self filteredSearch];
return self.filteredSearch.count;
}
如何解決此問題?我曾嘗試使用自我匹配和其他NSPredicate格式,但它們似乎不起作用。
您確定self.name是您希望搜索的對象的數組嗎? – danh
「搜索有效,但不會返回適當的結果」_that_是什麼意思?顯示'self.name'是什麼,你期望得到什麼結果,你得到了什麼結果。 – matt
@matt我編輯了這個問題 – aisha714