【发布时间】:2016-02-27 16:31:12
【问题描述】:
我想在我的 tableView 中使用搜索栏。 我有一个带有 Person (NSObject) 的 NSArray。在 cellForRow 我使用这个代码:
Person *person = [[self.sectionedPersonName objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text = person.fullName;
对于 searchBar 这个代码:
- (void)searchForText:(NSString *)searchText {
if (searchText.length > 0) {
searchBar = YES;
} else {
searchBar = NO;
}
NSLog(@"%@", self.sectionedPersonName);
NSPredicate *predicate; = [NSPredicate predicateWithFormat:@"SELF.fullName beginswith[c] %@", searchText];
self.searchResults = [self.sectionedPersonName filteredArrayUsingPredicate:predicate];
}
self.sectionedPersonName 包含这个:
(
(
"<Person: 0x7fc0f6012650>"
),
(
),
(
),
(
"<Person: 0x7fc0f600f8a0>",
"<Person: 0x7fc0f60132e0>"
),
(
),
(
),
(
),
(
"<Person: 0x7fc0f6012d80>"
),
(
),
(
"<Person: 0x7fc0f6012b30>"
),
(
"<Person: 0x7fc0f6010100>"
),
(
),
(
),
(
),
(
),
(
),
(
),
(
),
(
),
(
),
(
),
(
),
(
),
(
),
(
),
(
),
(
)
)
所以我的问题是如何对此进行 NSPredicate ? 这个我试过了,还是不行:
NSPredicate *predicate; = [NSPredicate predicateWithFormat:@"SELF.fullName beginswith[c] %@", searchText];
错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't do a substring operation with something that isn't a string (lhs = (
"Anna Haro"
) rhs = )'
【问题讨论】:
标签: ios objective-c nsarray uisearchbar nspredicate