2

I have a UITableView which is populated with an array of objects of type 'Product' which each have a 'name' attribute of type string. When a cell is clicked, a modal view is shown with more information on that product.

What I would like to do is split the products up into sections depending on the first letter of their name attribute. (Like iPhone contacts).

I have been struggling with this for a while now, and I can't seem to find anything online that solves my particular problem..

Could someone point me in the right direction?

2 Answers 2

5

There are several good examples here on SO. Here is a good example:

How to sort an NSMutableArray with custom objects in it?

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that has shown me how to order the array alphabetically, but I'm still struggling to display the now organised array in a UITableView with alphabetical sections?
If you implement the UITableViewDataSource protocol, you can define the number of rows / sections and what cell shows up at each point. You would use this sorted array to know what to present. You can see the protocol here: developer.apple.com/library/ios/#documentation/uikit/reference/…
In addition, if you want the alphabetical index - see the answer here: stackoverflow.com/questions/1655119/…
3

Use NSSortDescriptor like this..

NSSortDescriptor *sortDescriptor =
    [NSSortDescriptor sortDescriptorWithKey:@"name"
                                  ascending:YES
                                   selector:@selector(caseInsensitiveCompare:)];
NSArray *sortedArray = [nameOfArray sortedArrayUsingDescriptors:@[sortDescriptor]];

Hope, this is what you're looking for. Any concern get back to me. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.