【问题标题】:How to reverse the data in NSMutableArray? [duplicate]如何反转 NSMutableArray 中的数据? [复制]
【发布时间】:2011-11-26 04:52:13
【问题描述】:

可能重复:
How to display an array in reverse order in objective C

我有一个NSMutableArray,这个数组很好地包含UITableView 中的信息。 但我想先在UITableView 中显示最新信息。现在最早的信息首先出现在UITableView。我的代码如下:

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

那么我该如何反转NSMutableArray中的信息呢?

【问题讨论】:

    标签: iphone objective-c xcode ios4 nsmutablearray


    【解决方案1】:

    倒序枚举entries的内容怎么样?

    for (RSSEntry *entry in [entries reverseObjectEnumerator]) {
        ...
    }
    

    如果你只想取一个数组并创建一个反转数组,你可以这样做:

    NSArray *reversedEntries = [[entries reverseObjectEnumerator] allObjects];
    

    【讨论】:

    • 当然,如果你真的需要一个与原始数组相反的数组,只需在反向枚举时填充第二个可变数组。
    【解决方案2】:

    你可以这样试试:

    for (int k = [originalArray count] - 1; k >= 0; k--) {
        [reverseArray addObject:[originalArray objectAtIndex:k]];
    }
    

    【讨论】:

    • 我发现唯一适用于 nsmutablearray 的解决方案。谢谢!
    猜你喜欢
    • 2011-01-05
    • 2011-08-11
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    相关资源
    最近更新 更多