我已经在谷歌的土地上进行了广泛搜索,我已经找到了这方面的教程,但它们已经在IOS8之前了。我试图尽可能地将它拼凑在一起,但是当应用程序运行时,我在搜索栏中输入单词,没有任何返回,或者它崩溃了。所以这里是视图的外观,以及每个对象的含义:
这是我的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
我找不到我错的地方。任何帮助都将非常感激。或者,如果有什么可以指出我正确的方向。
答案 0 :(得分:1)
几个月前我遇到了问题,所以下面是一个真正帮助我的项目。也希望你。
https://github.com/mwazir2/ParseSearchNoPagination
喝彩!
更新
在我对上面这个例子的改编中,我遵循了项目中的所有内容。该项目使用Parse SDK中的PFQueryTableViewController,而不是默认的iOS TableViewController。
在第一部分 - (id)initWithCoder我评论了
//self.textKey = @"username";
然后我以某种方式改变了 - (PFQuery *)queryForTable方法。一个只是将whereKey更改为适合我的项目,第二个更改了查询一点,因为它应该只显示在与特定用户相关的表对象中。以下是各自的查询。
第一个更通用的查询
- (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) {
干杯!