开发者

How to display an array in reverse order in objective C [duplicate]

开发者 https://www.devze.com 2023-02-03 14:54 出处:网络
This question already has answers here: 开发者_开发百科How can I reverse a NSArray in Objective-C?
This question already has answers here: 开发者_开发百科 How can I reverse a NSArray in Objective-C? (18 answers) Closed 9 years ago.

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.


Use -[NSArray reverseObjectEnumerator]:

NSArray * a = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
NSArray * reverse = [[a reverseObjectEnumerator] allObjects];
NSLog(@"%@", reverse);  //logs "c, b, a"


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.


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];
0

精彩评论

暂无评论...
验证码 换一张
取 消