使用解析数据实现搜索栏的问题
Problems Implementing Search Bar with Parse Data
我在 Google 的土地上到处搜索,找到了这方面的教程,但它们是在 IOS8 之前的。我已尽我所能将它们拼凑在一起,但是当应用程序运行时,我在搜索栏中输入了单词,没有 returns,否则它会崩溃。下面是视图的外观以及每个对象的含义:
这是我的 JobListViewController.m 文件:
#import "JobDetailViewController.h"
#import "JobListViewController.h"
#import "Job.h"
#import "SearchedResultCell.h"
@interface JobListViewController () <UISearchDisplayDelegate, UISearchBarDelegate> {
}
@property (nonatomic, weak) IBOutlet UISearchBar *searchedBar;
@property (nonatomic, strong) NSString *mainTitle;
@property (nonatomic, strong) NSString *subTitle;
@property (nonatomic, assign) BOOL canSearch;
@end
@interface JobListViewController ()
@end
@implementation JobListViewController
{
NSArray *jobs;
NSArray *searchResults;
}
@synthesize searchedBar;
@synthesize mainTitle;
@synthesize subTitle;
@synthesize canSearch;
- (id)initWithCoder:(NSCoder *)aCoder
{
self = [super initWithCoder:aCoder];
if (self) {
// Custom the table
// The className to query on
self.parseClassName = @"Jobs";
// The key of the PFObject to display in the label of the default cell style
self.textKey = @"Position";
// Whether the built-in pull-to-refresh is enabled
self.pullToRefreshEnabled = YES;
// Whether the built-in pagination is enabled
self.paginationEnabled = YES;
// The number of objects to show per page
self.objectsPerPage = 10;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.searchedBar becomeFirstResponder];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.canSearch = 0;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)objectsWillLoad {
[super objectsWillLoad];
// This method is called before a PFQuery is fired to get more objects
}
- (PFQuery *)queryForTable
{
PFQuery *query;
if (self.canSearch == 0)
{
query = [PFQuery queryWithClassName:@"Jobs"];
}
else
{
query = [PFQuery queryWithClassName:@"Jobs"];
NSString *searchThis = [searchedBar.text uppercaseString];
[query whereKey:@"Position" containsString:searchThis];
}
[query orderByAscending:@"Position"];
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
return query;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object: (PFObject *)object
{
static NSString *simpleTableIdentifier = @"JobCell";
static NSString *pimpleTableIdentifier = @"JobCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
SearchedResultCell *cell = [self.tableView dequeueReusableCellWithIdentifier:pimpleTableIdentifier];
if (cell == nil) {
cell = [[SearchedResultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:pimpleTableIdentifier];
}
[self configureSearchResult:cell atIndexPath:indexPath object:object];
}
// Configure the cell
PFFile *thumbnail = [object objectForKey:@"imageFile"];
PFImageView *thumbnailImageView = (PFImageView*)[cell viewWithTag:100];
thumbnailImageView.image = [UIImage imageNamed:@"placeholder.jpg"];
thumbnailImageView.file = thumbnail;
[thumbnailImageView loadInBackground];
UILabel *positionLabel = (UILabel*) [cell viewWithTag:101];
positionLabel.text = [object objectForKey:@"Position"];
UILabel *rotationLabel = (UILabel*) [cell viewWithTag:102];
rotationLabel.text = [object objectForKey:@"Rotation"];
UILabel *locationLabel = (UILabel*) [cell viewWithTag:103];
locationLabel.text = [object objectForKey:@"Location"];
UILabel *typeLabel = (UILabel*) [cell viewWithTag:104];
typeLabel.text = [object objectForKey:@"Type"];
return cell;
}
- (void) objectsDidLoad:(NSError *)error
{
[super objectsDidLoad:error];
NSLog(@"error: %@", [error localizedDescription]);
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showJobDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
Job *job = [[Job alloc] init];
JobDetailViewController *destViewController = segue.destinationViewController;
PFObject *object = [self.objects objectAtIndex:indexPath.row];
job.position = [object objectForKey:@"Position"];
job.name = [object objectForKey:@"Name"];
job.email = [object objectForKey:@"Email"];
job.phone = [object objectForKey:@"Phone"];
job.imageFile = [object objectForKey:@"imageFile"];
job.rotation = [object objectForKey:@"Rotation"];
job.location = [object objectForKey:@"Location"];
job.type = [object objectForKey:@"Type"];
job.clearance = [object objectForKey:@"Clearance"];
job.job_description = [object objectForKey:@"Job_Description"];
job.qualifications = [object objectForKey:@"Qualifications"];
destViewController.job = job;
}
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self clear];
self.canSearch = 1;
[self.searchedBar resignFirstResponder];
[self queryForTable];
[self loadObjects];
}
/*
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (searchResults == nil) {
return 0;
} else if ([searchResults count] == 0) {
return 1;
} else {
return [self.objects count];
}
}
*/
- (void)configureSearchResult:(SearchedResultCell *)cell atIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
mainTitle = [object objectForKey:@"Position"];
cell.mainTitle.text = mainTitle;
subTitle = [object objectForKey:@"Type"];
cell.detail.text = subTitle;
// Implement this if you want to Show image
cell.showImage.image = [UIImage imageNamed:@"placeholder.jpg"];
PFFile *imageFile = [object objectForKey:@"imageFile"];
if (imageFile) {
cell.showImage.file = imageFile;
[cell.showImage loadInBackground];
}
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[searchedBar resignFirstResponder];
if ([self.objects count] == indexPath.row) {
[self loadNextPage];
} else {
PFObject *photo = [self.objects objectAtIndex:indexPath.row];
NSLog(@"%@", photo);
// Do something you want after selected the cell
}
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[self.searchedBar resignFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar {
[self.searchedBar resignFirstResponder];
[self queryForTable];
[self loadObjects];
}
@end
我找不到哪里出错了。任何帮助将不胜感激。或者如果你能给我指出正确的方向的话。
几个月前我遇到了问题,所以下面是一个真正帮助我的项目。也希望你。
https://github.com/mwazir2/ParseSearchNoPagination
干杯!
更新
在我对上面这个例子的改编中,我遵循了项目中的所有内容。该项目使用 Parse SDK 中的 PFQueryTableViewController 而不是默认的 iOS TableViewController。
在第一部分 -(id)initWithCoder 我评论了
//self.textKey = @"username";
然后我在某些方面改变了 - (PFQuery *)queryForTable 方法。一个只是更改了 whereKey 以适应我的项目,第二个更改了查询更多一点,因为它应该只显示在与特定用户相关的 table 对象中。以下是相应的查询。
第一个更一般的查询
- (PFQuery *)queryForTable
{
PFQuery *query = [PFUser query];
if (self.canSearch == 0) {
query = [PFQuery queryWithClassName:@"_User"];
} else {
query = [PFQuery queryWithClassName:@"_User"];
NSString *searchThis = [searchedBar.text capitalizedString];
//PFQuery *query = [PFQuery queryWithClassName:@"Channel"];
//[query whereKey:@"channelName" equalTo:searchThis];
[query whereKey:@"username" containsString:searchThis];
}
第二个,特定于当前用户:
- (PFQuery *)queryForTable
{
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
[query whereKey:@"owner" equalTo:[PFUser currentUser]];
[query orderByDescending:@"createdAt"];
if (self.pullToRefreshEnabled) {
query.cachePolicy = kPFCachePolicyNetworkOnly;
}
// If no objects are loaded in memory, we look to the cache first to fill the table
// and then subsequently do a query against the network.
if (self.objects.count == 0) {
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
}
return query;
}
对于 tableView 方法,我只使用了:tableView cellForRowAtIndexPath(与您的非常相似)。还有 LoadMoreCell 的 cellForRowAtIndexPath,就像示例中一样。
还有 tableView heightForRowAtIndexPath:对于 LoadMoreCell 和 tableView didSelectRowAtIndexPath,如下所示:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//[searchedBar resignFirstResponder];
if ([self.objects count] == indexPath.row) {
[self loadNextPage];
} else {
PFObject *photo = [self.objects objectAtIndex:indexPath.row];
NSLog(@"%@", photo);
// Do something you want after selected the cell
}
}
当然可以在 }else{ 语句下面更改它或删除 NSLog 行。
我建议你尝试用你的对象和键改变例子本身,直到它起作用然后改变你的,就像例子一样......:)
复合查询
PFQuery *query1 = [PFQuery queryWithClassName:@"Class1"];
[name whereKey:@"column1" equalTo:[fieldInCode text]];
PFQuery *query2 = [PFQuery queryWithClassName:@"Class1"];
[enterKey whereKey:@"column2" equalTo:[anotherField text]];
PFQuery *query = [PFQuery orQueryWithSubqueries:@[query1,query2]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
干杯!
我在 Google 的土地上到处搜索,找到了这方面的教程,但它们是在 IOS8 之前的。我已尽我所能将它们拼凑在一起,但是当应用程序运行时,我在搜索栏中输入了单词,没有 returns,否则它会崩溃。下面是视图的外观以及每个对象的含义:
这是我的 JobListViewController.m 文件:
#import "JobDetailViewController.h"
#import "JobListViewController.h"
#import "Job.h"
#import "SearchedResultCell.h"
@interface JobListViewController () <UISearchDisplayDelegate, UISearchBarDelegate> {
}
@property (nonatomic, weak) IBOutlet UISearchBar *searchedBar;
@property (nonatomic, strong) NSString *mainTitle;
@property (nonatomic, strong) NSString *subTitle;
@property (nonatomic, assign) BOOL canSearch;
@end
@interface JobListViewController ()
@end
@implementation JobListViewController
{
NSArray *jobs;
NSArray *searchResults;
}
@synthesize searchedBar;
@synthesize mainTitle;
@synthesize subTitle;
@synthesize canSearch;
- (id)initWithCoder:(NSCoder *)aCoder
{
self = [super initWithCoder:aCoder];
if (self) {
// Custom the table
// The className to query on
self.parseClassName = @"Jobs";
// The key of the PFObject to display in the label of the default cell style
self.textKey = @"Position";
// Whether the built-in pull-to-refresh is enabled
self.pullToRefreshEnabled = YES;
// Whether the built-in pagination is enabled
self.paginationEnabled = YES;
// The number of objects to show per page
self.objectsPerPage = 10;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.searchedBar becomeFirstResponder];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.canSearch = 0;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)objectsWillLoad {
[super objectsWillLoad];
// This method is called before a PFQuery is fired to get more objects
}
- (PFQuery *)queryForTable
{
PFQuery *query;
if (self.canSearch == 0)
{
query = [PFQuery queryWithClassName:@"Jobs"];
}
else
{
query = [PFQuery queryWithClassName:@"Jobs"];
NSString *searchThis = [searchedBar.text uppercaseString];
[query whereKey:@"Position" containsString:searchThis];
}
[query orderByAscending:@"Position"];
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
return query;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object: (PFObject *)object
{
static NSString *simpleTableIdentifier = @"JobCell";
static NSString *pimpleTableIdentifier = @"JobCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
SearchedResultCell *cell = [self.tableView dequeueReusableCellWithIdentifier:pimpleTableIdentifier];
if (cell == nil) {
cell = [[SearchedResultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:pimpleTableIdentifier];
}
[self configureSearchResult:cell atIndexPath:indexPath object:object];
}
// Configure the cell
PFFile *thumbnail = [object objectForKey:@"imageFile"];
PFImageView *thumbnailImageView = (PFImageView*)[cell viewWithTag:100];
thumbnailImageView.image = [UIImage imageNamed:@"placeholder.jpg"];
thumbnailImageView.file = thumbnail;
[thumbnailImageView loadInBackground];
UILabel *positionLabel = (UILabel*) [cell viewWithTag:101];
positionLabel.text = [object objectForKey:@"Position"];
UILabel *rotationLabel = (UILabel*) [cell viewWithTag:102];
rotationLabel.text = [object objectForKey:@"Rotation"];
UILabel *locationLabel = (UILabel*) [cell viewWithTag:103];
locationLabel.text = [object objectForKey:@"Location"];
UILabel *typeLabel = (UILabel*) [cell viewWithTag:104];
typeLabel.text = [object objectForKey:@"Type"];
return cell;
}
- (void) objectsDidLoad:(NSError *)error
{
[super objectsDidLoad:error];
NSLog(@"error: %@", [error localizedDescription]);
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showJobDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
Job *job = [[Job alloc] init];
JobDetailViewController *destViewController = segue.destinationViewController;
PFObject *object = [self.objects objectAtIndex:indexPath.row];
job.position = [object objectForKey:@"Position"];
job.name = [object objectForKey:@"Name"];
job.email = [object objectForKey:@"Email"];
job.phone = [object objectForKey:@"Phone"];
job.imageFile = [object objectForKey:@"imageFile"];
job.rotation = [object objectForKey:@"Rotation"];
job.location = [object objectForKey:@"Location"];
job.type = [object objectForKey:@"Type"];
job.clearance = [object objectForKey:@"Clearance"];
job.job_description = [object objectForKey:@"Job_Description"];
job.qualifications = [object objectForKey:@"Qualifications"];
destViewController.job = job;
}
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self clear];
self.canSearch = 1;
[self.searchedBar resignFirstResponder];
[self queryForTable];
[self loadObjects];
}
/*
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (searchResults == nil) {
return 0;
} else if ([searchResults count] == 0) {
return 1;
} else {
return [self.objects count];
}
}
*/
- (void)configureSearchResult:(SearchedResultCell *)cell atIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
mainTitle = [object objectForKey:@"Position"];
cell.mainTitle.text = mainTitle;
subTitle = [object objectForKey:@"Type"];
cell.detail.text = subTitle;
// Implement this if you want to Show image
cell.showImage.image = [UIImage imageNamed:@"placeholder.jpg"];
PFFile *imageFile = [object objectForKey:@"imageFile"];
if (imageFile) {
cell.showImage.file = imageFile;
[cell.showImage loadInBackground];
}
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[searchedBar resignFirstResponder];
if ([self.objects count] == indexPath.row) {
[self loadNextPage];
} else {
PFObject *photo = [self.objects objectAtIndex:indexPath.row];
NSLog(@"%@", photo);
// Do something you want after selected the cell
}
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[self.searchedBar resignFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar {
[self.searchedBar resignFirstResponder];
[self queryForTable];
[self loadObjects];
}
@end
我找不到哪里出错了。任何帮助将不胜感激。或者如果你能给我指出正确的方向的话。
几个月前我遇到了问题,所以下面是一个真正帮助我的项目。也希望你。
https://github.com/mwazir2/ParseSearchNoPagination
干杯!
更新
在我对上面这个例子的改编中,我遵循了项目中的所有内容。该项目使用 Parse SDK 中的 PFQueryTableViewController 而不是默认的 iOS TableViewController。
在第一部分 -(id)initWithCoder 我评论了
//self.textKey = @"username";
然后我在某些方面改变了 - (PFQuery *)queryForTable 方法。一个只是更改了 whereKey 以适应我的项目,第二个更改了查询更多一点,因为它应该只显示在与特定用户相关的 table 对象中。以下是相应的查询。
第一个更一般的查询
- (PFQuery *)queryForTable
{
PFQuery *query = [PFUser query];
if (self.canSearch == 0) {
query = [PFQuery queryWithClassName:@"_User"];
} else {
query = [PFQuery queryWithClassName:@"_User"];
NSString *searchThis = [searchedBar.text capitalizedString];
//PFQuery *query = [PFQuery queryWithClassName:@"Channel"];
//[query whereKey:@"channelName" equalTo:searchThis];
[query whereKey:@"username" containsString:searchThis];
}
第二个,特定于当前用户:
- (PFQuery *)queryForTable
{
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
[query whereKey:@"owner" equalTo:[PFUser currentUser]];
[query orderByDescending:@"createdAt"];
if (self.pullToRefreshEnabled) {
query.cachePolicy = kPFCachePolicyNetworkOnly;
}
// If no objects are loaded in memory, we look to the cache first to fill the table
// and then subsequently do a query against the network.
if (self.objects.count == 0) {
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
}
return query;
}
对于 tableView 方法,我只使用了:tableView cellForRowAtIndexPath(与您的非常相似)。还有 LoadMoreCell 的 cellForRowAtIndexPath,就像示例中一样。
还有 tableView heightForRowAtIndexPath:对于 LoadMoreCell 和 tableView didSelectRowAtIndexPath,如下所示:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//[searchedBar resignFirstResponder];
if ([self.objects count] == indexPath.row) {
[self loadNextPage];
} else {
PFObject *photo = [self.objects objectAtIndex:indexPath.row];
NSLog(@"%@", photo);
// Do something you want after selected the cell
}
}
当然可以在 }else{ 语句下面更改它或删除 NSLog 行。
我建议你尝试用你的对象和键改变例子本身,直到它起作用然后改变你的,就像例子一样......:)
复合查询
PFQuery *query1 = [PFQuery queryWithClassName:@"Class1"];
[name whereKey:@"column1" equalTo:[fieldInCode text]];
PFQuery *query2 = [PFQuery queryWithClassName:@"Class1"];
[enterKey whereKey:@"column2" equalTo:[anotherField text]];
PFQuery *query = [PFQuery orQueryWithSubqueries:@[query1,query2]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
干杯!