Possible Duplicate:
How to display an array in reverse order in objective C
I have an NSMutableArray
and this array contains information nicely in UITableView
.
but I want to display latest information first in UITableView
. Right now the earliest information comes first in UITableView
. My code is as follows:
NSMutableArray *entries = [NSMutableArray array];
[self parseFeed:doc.rootElement entries:entries];
for (RSSEntry *entry in entries) {
[allEntries insertObject:entry atIndex:0]; //insertIdx];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
}
then How can I reverse the information in NSMutableArray
?
In the Swift array, we can reverse the array. To reverse the array we use the reverse() function. This function reverses the order of the specified array. It is the easiest way to reverse the array.
The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray . NSMutableArray is “toll-free bridged” with its Core Foundation counterpart, CFMutableArray .
How about just enumerating the contents of entries
in reverse order?
for (RSSEntry *entry in [entries reverseObjectEnumerator]) {
...
}
If you just want to take an array and create a reversed array, you can do this:
NSArray *reversedEntries = [[entries reverseObjectEnumerator] allObjects];
You can try like this way:
for (int k = [originalArray count] - 1; k >= 0; k--) {
[reverseArray addObject:[originalArray objectAtIndex:k]];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With