0

I have searched the land of Google far and wide and I have found tutorials on this, but they are before IOS8. I have tried to piece this together as best as I can, but yet when the app runs, and I type words into the search bar, nothing returns, or it crashes. So here's how the view looks, and what each object means:

screenshot

Here is my JobListViewController.m File:

#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

I cannot find where I am going wrong. Any help whatsoever would be much appreciated. Or if anything if you can point me in the right direction.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Joshua Hart
  • 772
  • 1
  • 21
  • 31

1 Answers1

1

I had problems with that I few months ago, so below is a project that reeeally helped me. Hope for you too.

https://github.com/mwazir2/ParseSearchNoPagination

cheers!

Update

In my adaptation of this example above I followed everything from the project. The project uses PFQueryTableViewController from Parse SDK and not the default iOS TableViewController.

In the first part -(id)initWithCoder I have commented the

//self.textKey = @"username";

Then I altered the - (PFQuery *)queryForTable method in some ways. One just changed the whereKey to adequate my project and the second changed the query a little bit more because it should only show in the table objects related to specific users. Below follows respective queries.

The first more general query

- (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];

}

The second, specific to current user:

- (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;
}

For the tableView methods I only used: tableView cellForRowAtIndexPath(pretty much like yours). And also the cellForRowAtIndexPath for the LoadMoreCell, like in the example.

Also the tableView heightForRowAtIndexPath: for the LoadMoreCell and tableView didSelectRowAtIndexPath, like below:

 - (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
}
}

Of course below the } else { statement you can change it or delete the NSLog line.

I suggest you try to alter the example itself with your object and keys, until it works an then alter yours, exactly like the example... :)

Compound Query

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) {

Cheers!

  • Thank you, i followed their coding and it's gotten me further along, however when I enter text into the search bar it doesn't work. I'm thinking it's my PFQuery method queryForTable. I tell it to look in my Class called Jobs and I want it to specifically search the content of my Key called Position. However it doesn't do it. – Joshua Hart Jan 28 '15 at 14:21
  • Hi buddy! Im in a bit of a hurry now, in probably 8h I comeback here and edit my fisry answer with some of the things that I updated from this code example that I pointed to you. Just to know, your ACL column in Parse is set to public read and write ? If its in private that query would t work... –  Jan 28 '15 at 14:46
  • No problem! Yes my ACL Column is set to public read and write. – Joshua Hart Jan 28 '15 at 16:02
  • Are using PFQuerryTavleViewController or the standard TableViewController ? –  Jan 28 '15 at 22:26
  • Could you add another answer? I cannot believe how stupid my mistake was....uppercaseString, lowercaseString and capitalizedString will return different values! In my case, all I had to do was change it to capitalizedString! Blah! Thank you! – Joshua Hart Jan 31 '15 at 13:36
  • Yes! They return...I will edit the first answer later. Congrats! Cheers –  Jan 31 '15 at 13:43
  • Question: If I wanted to search another classkey instead of Position...how would I implement that? Say I wanted to search by Type too? At the same time... – Joshua Hart Jan 31 '15 at 14:07
  • 1
    I dont think it searchs more than one key at the same time. But it searchs relation between keys...In my answer the second query uses that relational feature. But you must set your objects and code to work and have any relantion. In Parse documentation they explain more about that. Also I recomend downloading some of their project tutorials, because they implement those kind of relations –  Jan 31 '15 at 14:14
  • 1
    Hi @DiplomaticMedic! I did some research and found the type of query that enables search two keys at the same type. Its what Parse call a Compound queries. Its described in their documentation, last item in Queries section. I will update my answer with the compound query. Cheers! –  Feb 01 '15 at 12:50