-1

I am building a database style app using Parse in Xcode 5 and want to be able to order all the cells by date created. I also want to then be able to change this to alphabetical order when I click an option of a segmented control (by date order being the default). How do I do this?

My table view controller (.m):

#import "JKENotesListViewController.h"
#import "JKENotesViewController.h"
#import <Parse/Parse.h>

@interface JKENotesListViewController ()

@end

@implementation JKENotesListViewController

- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithClassName:@"Post"];
self = [super initWithCoder:aDecoder];
if (self) {
    // The className to query on
    self.parseClassName = @"Post";

    // 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 = 15;
}
return self;
}

#pragma mark - UIViewController

- (void)viewDidLoad {
[super viewDidLoad];

PFUser *currentUser = [PFUser currentUser];
if (currentUser) {
    NSLog(@"Current user: %@", currentUser.username);
}
   else {
    [self performSegueWithIdentifier:@"showLogin" sender:self];
}    

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self loadObjects];
}

#pragma mark - PFQueryTableViewController

// Override to customize the look of a cell representing an object. The default is to display
// a UITableViewCellStyleDefault style cell with the label being the textKey in the object,
// and the imageView being the imageKey in the object.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath object:(PFObject *)object {
    static NSString *CellIdentifier = @"Cell";

    PFTableViewCell *cell = (PFTableViewCell *)[tableView     dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier];
    }

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEEE, MMMM d yyyy"];
    NSDate *date = [object createdAt];

    // Configure the cell
    cell.textLabel.text = [object objectForKey:@"title"];
    cell.detailTextLabel.text = [dateFormatter stringFromDate:date];
    cell.imageView.image = [UIImage imageNamed:@"bike-iconcell.png"];

    return cell;
}

// Override to customize what kind of query to perform on the class. The default is to query for
// all objects ordered by createdAt descending.
- (PFQuery *)queryForTable {

    // Create a query
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];

// Follow relationship
if ([PFUser currentUser]) {
    [query whereKey:@"author" equalTo:[PFUser currentUser]];
}
else {
    // I added this so that when there is no currentUser, the query will not return any data
    // Without this, when a user signs up and is logged in automatically, they briefly see a table with data
    // before loadObjects is called and the table is refreshed.
    // There are other ways to get an empty query, of course. With the below, I know that there
    // is no such column with the value in the database.
    [query whereKey:@"nonexistent" equalTo:@"doesn't exist"];
    }

    return query;
}   


#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"showNote"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        PFObject *object = [self.objects objectAtIndex:indexPath.row];

        JKENotesViewController *note = (JKENotesViewController *)segue.destinationViewController;
        note.note = object;
    }
}

- (IBAction)logout:(id)sender {
    [PFUser logOut];
    [self performSegueWithIdentifier:@"showLogin" sender:self];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
} 

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {

    // Remove the row from data model
    PFObject *object = [self.objects objectAtIndex:indexPath.row];
    [object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        [self refreshControl];
        [tableView reloadData];



    }];
    }
}
@end

Many thanks in advance!

1 Answers1

0

As a guideline, you have to sort you datasource (probably NS(Mutable)Array or NS(Mutable)Dictionary) as per your criteria and once the datasource is sorted out you need to call:

[yourTableView reloadData];

Later when you have to change the sort criteria to alphabetic order (may be through segmented control) sort data source in alphabetic order instead of date created, and don;t forget to reload your table again.

Below are some examples:

Sort in descending order Sort using custom objects

Community
  • 1
  • 1
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
  • How do I make the them NSArrays? – user3762914 Jul 11 '14 at 04:11
  • Oh, so how do I order all my entries in alphabetical order? – user3762914 Jul 12 '14 at 02:30
  • @user3762914, that is the actual thing you need to focus, if resultset is sorted in the datasource in *alphabetic order* then the cell will automatically appear to be sorted on the next reload. To sort in alphabetic order follow the first link and in comparer you can do a string comparison(possibly) to determine which datasource item should appear first. – NeverHopeless Jul 12 '14 at 19:08