Click here to Skip to main content
11,409,766 members (55,986 online)
Click here to Skip to main content

Sorting it out: How to sort a TableView

, 12 Nov 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Some way to sort in a table.

Introduction

We wanted to know how to sort some table values, which somehow in a strange oder.

Background

It is nice to have some sorting via some algorithm, but almost everybody has it's own favourites and so needs some manual interaction. But how does it work.

A look under the hood

The code is somehow "boilerplate" to interact in the MVC design pattern. I strong recommend to understand the MVC, because allmost every serious objective-C code is build on it.

In my sample code are 3 different sorting ways implemented.

1. Sorting by some algorithm

This implementation follows strongly the MVC pattern: The Controller fires to the Model which sortes the data and the View is showing it. For sanity I have used the simplest String Comparison. There could also be other function. 

//Action from the view
- (IBAction)onButtonAtoZ:(id)sender
{
	NSLog(@"%s",__func__);
	//do sort: changing the Model data
	[_data sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
	//show data (present the changed Model)
	[self.tableView reloadData];
}

Not bad for 3 lines of code, but sorting with other functions gets interesting, when the model data isnt not only a string, but a complex object. Like an person or customer. Sorting for the distance or next birthday could be interesting options.

2. Sorting the "Apple way"

Apple provides a fine documentation on Managing the Reordering of Rows, but sometimes lack of implementing a working sample project. 

To set the table in the edit mode, we need to call:

[self.tableView setEditing:YES animated:YES];

But that only works when in the storyboard is properly set. You need to set the delegate and datasource in it. 

And than follow the implementation of the protocol pattern for the UITableViewDataSource protocol. I should mention that this an important method which is also part of the MVC pattern, by defining ways to interact between objects. It is all in my code and explained in the Apple documentation.

3. Ray Wenderlich

As often the people at Ray Wenderlich have done a fine job by providing a Cookbook: Moving Table View Cells with a Long Press Gesture. So I couldnt stand to present it.

To get it working this way, we need a LongGestureRecognizer. To separate this solution I add and remove it.

registeredLongPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognized:)];
		[self.tableView addGestureRecognizer:registeredLongPress];
		[_buttonLongPress setTitle:@"LongPress" forState:UIControlStateNormal];
	} else {
		NSLog(@"remove long press event");
		[self.tableView removeGestureRecognizer:registeredLongPress];

The hard work is done in the handler longPressGestureRecognized. I also like these animation. Fine job.

I see interesting potenzial to do some fancy drawing in the drag image.

Points of Interest

It is really a fine job to implement some sorting, so every user can experience its data as he wishes. Every sorting has its advantages and disadvantages. 

History

- Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

KarstenK
Software Developer
Germany Germany
I am living in germany and now living from programming for some Years. In my spare time I like sports as jogging, playing football (soccer) and basketball.

We must take care for our planet, because we and our family has no other. And everybody has to do something for it.

Comments and Discussions

 
GeneralMy vote of 5 PinprofessionalVolynsky Alex14-Nov-14 23:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150414.5 | Last Updated 12 Nov 2014
Article Copyright 2014 by KarstenK
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid