我通过执行以下操作解决了它(希望它可以帮助其他人):
我将 CoreData 中的 UICollectionView 填充到 NSArray 中:
self.allCVData = [context executeFetchRequest:fetchRequest error:&error];
然后将我的 NSArray 添加到 NSMutableArray 中,以便我可以使用它来过滤 UICollection:
self.filteredCVData = [[NSMutableArray alloc] initWithArray:allCVData];
添加了一个 UITextField 并将其命名为 searchField。
在 viewDidLoad 中添加了一个选择器:
[mytextfield addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
添加了一种方法来接受 UITextField 中的更改:
-(void) textDidChange:(id)sender
{
UITextField* searchField = (UITextField *) sender;
if(searchField.text.length == 0)
{
self.isFiltered = FALSE;
[self.filteredCVData removeAllObjects];
[self.filteredCVData addObjectsFromArray:self.allCVData];
}
else
{
self.isFiltered = true;
[self.filteredCVData removeAllObjects];
self.filteredCVData = [[NSMutableArray alloc] init];
LogInfo(@"Before Looping in allCVData Array.");
for (OLPerson* person in allCVData)
{
NSRange firstnameRange = [person.firstname rangeOfString:searchField.text options:NSCaseInsensitiveSearch];
NSRange surnameRange = [person.surname rangeOfString:searchField.text options:NSCaseInsensitiveSearch];
if(firstnameRange.location != NSNotFound || surnameRange.location != NSNotFound)
{
[self.filteredCVData addObject:person];
}
}
}
[self.collectionView reloadData];
}