Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / iOS

Sorting it out: How to sort a TableView

5.00/5 (1 vote)
12 Nov 2014CPOL2 min read 17.3K   41  
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. 

C++
//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)