I want to display data in UITableView in reverse order. I manage to do it with little effort but can any one provide a better way to do it. Thanks in advance.
Asked
Active
Viewed 7,363 times
4
-
3To think of a method better then yours it would be great what your method is... – Vladimir Jan 10 '11 at 08:06
-
I recommend updating your question with your attempt, otherwise you're unlikely to get much of a response. – John Parker Jan 10 '11 at 08:07
3 Answers
31
Use -[NSArray reverseObjectEnumerator]
:
NSArray * a = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
NSArray * reverse = [[a reverseObjectEnumerator] allObjects];
NSLog(@"%@", reverse); //logs "c, b, a"

Dave DeLong
- 242,470
- 58
- 448
- 498
15
Well i dont know what your code is but you can do something like this
cell.title = [anarray objectAtIndex:(anarray.count - indexPath.row - 1)];
Hope this may works. Cheers.

Dave DeLong
- 242,470
- 58
- 448
- 498

Robin
- 10,011
- 5
- 49
- 75
-
2Fixed formatting and changed an erroneous set of `[`...`]` to `(`...`)` – Dave DeLong Jan 10 '11 at 08:09
2
There is a much easier solution, if you take advantage of the built-in reverseObjectEnumerator method on NSArray, and the allObjects method of NSEnumerator:
NSArray* reversedArray = [[startArray reverseObjectEnumerator] allObjects];

Zagham Arshad
- 111
- 1
- 7
-
1That *is* a pretty good solution, but I notice that Dave DeLong suggested the same thing 2.5 years ago. Did you miss his answer? – Caleb Jul 09 '13 at 04:21
-
1Its also an exact copy and paste of this answer by somebody else from almost 4 years ago here: http://stackoverflow.com/a/586529/1026573 – James Kuang Dec 10 '14 at 21:04