Ios PFObject比较:选项:搜索parse.com类时出现范围错误
我试图通过UISearchBar从我的集合视图中的Parse.com类进行搜索查询。当我尝试搜索时,它崩溃并给出下面的错误。是否有更好的方法对集合视图执行搜索功能,如果没有,我做错了什么Ios PFObject比较:选项:搜索parse.com类时出现范围错误,ios,parse-platform,uicollectionview,uisearchbar,Ios,Parse Platform,Uicollectionview,Uisearchbar,我试图通过UISearchBar从我的集合视图中的Parse.com类进行搜索查询。当我尝试搜索时,它崩溃并给出下面的错误。是否有更好的方法对集合视图执行搜索功能,如果没有,我做错了什么 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PFObject compare:options:range:]: unrecognized selector sent to instance
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PFObject compare:options:range:]: unrecognized selector sent to instance 0x1741366c0'
代码如下:
@implementation DiscoverViewController
- (void)viewDidLoad {
[super viewDidLoad];
filteredContentList = [[NSMutableArray alloc] init];
[self fetchFeatured];
_featured = [[NSMutableArray alloc] initWithCapacity:1000];
}
- (void)fetchFeatured {
PFQuery *query = [PFQuery queryWithClassName:@"Featured"];
[query findObjectsInBackgroundWithBlock:^(NSArray *posts, NSError *error) {
if (!error) {
} else {
NSLog(@"Error fetching featured");
}
[_featured setArray:posts];
[_collectionView reloadData];
}];
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (isSearching) {
return [filteredContentList count];
} else {
return [self.featured count];
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if (isSearching) {
DiscoverTableViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiscoverTableViewCell" forIndexPath:indexPath];
_featuredObject = [_featured objectAtIndex:indexPath.row];
cell.name.text = _featuredObject[@"name"];
[(PFFile*)_featuredObject[@"profilePic"] getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
cell.profilePic.image = [UIImage imageWithData:data];
}];
return cell;
}
else {
DailyDiscoverTableViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DailyDiscoverTableViewCell" forIndexPath:indexPath];
_featuredObject = [_featured objectAtIndex:indexPath.row];
cell.name.text = _featuredObject[@"name"];
[(PFFile*)_featuredObject[@"profilePic"] getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
cell.profilePic.image = [UIImage imageWithData:data];
}];
return cell;
}
}
- (void)searchTableList {
NSString *searchString = _searchBar.text;
for (NSString *tempStr in _featured) {
NSComparisonResult result = [tempStr compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchString length])];
if (result == NSOrderedSame) {
[filteredContentList addObject:tempStr];
}
}
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
isSearching = YES;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
[filteredContentList removeAllObjects];
if([searchText length] != 0) {
isSearching = YES;
[self searchTableList];
}
else {
isSearching = NO;
}
[_collectionView reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
NSLog(@"Cancel clicked");
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSLog(@"Search Clicked");
[self searchTableList];
}
@end
@implementation DiscoverViewController
-(无效)viewDidLoad{
[超级视图下载];
filteredContentList=[[NSMutableArray alloc]init];
[自我介绍];
_特色=[[NSMutableArray alloc]initWithCapacity:1000];
}
-(无效)删除{
PFQuery*query=[PFQuery queryWithClassName:@“特色”];
[查询findObjectsInBackgroundWithBlock:^(NSArray*posts,NSError*error){
如果(!错误){
}否则{
NSLog(@“错误获取功能”);
}
[_特征setArray:posts];
[_collectionView重新加载数据];
}];
}
#布拉格标记
-(NSInteger)collectionView中的节数:(UICollectionView*)collectionView{
返回1;
}
-(NSInteger)collectionView:(UICollectionView*)collectionView项目编号截面:(NSInteger)截面{
如果(正在搜索){
返回[filteredContentList计数];
}否则{
返回[自选计数];
}
}
-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath{
如果(正在搜索){
DiscoverTableViewCell*cell=[collectionView dequeueReusableCellWithReuseIdentifier:@“DiscoverTableViewCell”forIndexPath:indexPath];
_featuredObject=[\u特征对象索引:indexPath.row];
cell.name.text=_featuredObject[@“name”];
[(PFFile*)_featuredObject[@“profilePic”]getDataInBackgroundWithBlock:^(NSData*数据,NSError*错误){
cell.profilePic.image=[UIImage-imageWithData:data];
}];
返回单元;
}
否则{
DailyDiscoverTableViewCell*cell=[collectionView dequeueReusableCellWithReuseIdentifier:@“DailyDiscoverTableViewCell”forIndexPath:indexPath];
_featuredObject=[\u特征对象索引:indexPath.row];
cell.name.text=_featuredObject[@“name”];
[(PFFile*)_featuredObject[@“profilePic”]getDataInBackgroundWithBlock:^(NSData*数据,NSError*错误){
cell.profilePic.image=[UIImage-imageWithData:data];
}];
返回单元;
}
}
-(作废)可搜索列表{
NSString*searchString=\u searchBar.text;
用于(NSString*tempStr在_特色){
NSComparisonResult=[tempStr比较:搜索字符串选项:(NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch)范围:NSMakeRange(0,[searchString长度]);
如果(结果==SensorDeredName){
[filteredContentList addObject:tempStr];
}
}
}
-(无效)searchBarTextDidBeginEditing:(UISearchBar*)搜索栏{
isSearching=是;
}
-(无效)搜索栏:(UISearchBar*)搜索栏文本更改:(NSString*)搜索文本{
[filteredContentList removeAllObjects];
如果([searchText length]!=0){
isSearching=是;
[自查表];
}
否则{
isSearching=否;
}
[_collectionView重新加载数据];
}
-(无效)搜索栏取消按钮选中:(UISearchBar*)搜索栏{
NSLog(@“单击取消”);
}
-(无效)搜索栏搜索按钮选中:(UISearchBar*)搜索栏{
NSLog(@“已单击搜索”);
[自查表];
}
@结束
根据您的代码块:
for (NSString *tempStr in _featured) {
NSComparisonResult result = [tempStr compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchString length])];
if (result == NSOrderedSame) {
[filteredContentList addObject:tempStr];
}
}
我认为_特色数组是一个PFObjects数组。
看起来您正试图将PFObject隐式转换为NSString。例如,如果您的搜索功能正在搜索“名称”,则应与名称进行比较:
for (PFObject *tempObj in _featured) { // or perhaps Featured
NSComparisonResult result = [tempObj[@"name"] compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchString length])];
if (result == NSOrderedSame) {
[filteredContentList addObject:tempObj];
}
}
让我知道这对你有什么作用。实际上不起作用。它没有崩溃,但当我搜索时,所有相同的单元格仍然在那里哦,我找到了。在textdichange方法中,我编写了[self.tableView reloadData];,应该是[self.collectionView reloadData];。谢谢你,你的代码成功了!这是UICollectionView还是UITableView?太棒了,这就是我刚才看到的。好买卖!我将您的问题编辑为使用collectionView,因为这与您遇到的错误不太相关。嗯,但是现在由于某种原因,当我搜索时,它总是(而且只)返回collection视图中的第一行,即使对象不包含任何搜索到的字母。。。