2015-04-21 54 views
1

我想通過一個UISearchBar從我的集合視圖中的Parse.com類進行搜索查詢。當我嘗試搜索它崩潰並給我下面的錯誤。有沒有更好的方法來使集合視圖的搜索功能,如果沒有,我做錯了什麼?PFObject比較:選項:搜索parse.com類時的範圍錯誤

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 

回答

1

for (NSString *tempStr in _featured) { 
     NSComparisonResult result = [tempStr compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchString length])]; 
    if (result == NSOrderedSame) { 
     [filteredContentList addObject:tempStr]; 
    } 
} 

我想象_featured陣列是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]; 
    } 
} 

讓我知道那是你的作品。

+0

不實際工作。它不會崩潰,但是當我搜索時,所有相同的單元格仍然存在 – tracifycray

+0

哦,我找到了。在textDidChange方法中,我寫了[self.tableView reloadData] ;,它應該是[self.collectionView reloadData] ;.謝謝你,你的代碼工作! – tracifycray

+0

這是在UICollectionView或UITableView? – benhameen