NSMutableArray - 在开始时添加数组 [英] NSMutableArray - Add array at start

查看:28
本文介绍了NSMutableArray - 在开始时添加数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的下拉刷新案例.我已将数据加载到表中,并在后端有一个可变数据数组,我收到一个新数据数组,并希望在现有数组的开头添加这个完整数组.

It is a simple pull to refresh case. I have data loaded into table and have a mutable data array at back-end, I receive a array of new data and want to add this complete array at start of existing array.

一种解决方法是使用新到达的数据创建新数组,然后使用 addObjectsFromArray: 方法将以前的数组添加到其中.是否有一些解决方法可以将新数据数组直接添加到前一个数组的开头?

One workaround is to create new array with new arrived data and then add previous array into it using addObjectsFromArray: method. Is there some workaround to add new data array to the start of previous array directly?

推荐答案

首先,构建一个 NSIndexSet.

NSIndexSet *indexes = [NSIndexSet indexSetWithIndexesInRange:
    NSMakeRange(0,[newArray count])];

现在,使用 NSMutableArrayinsertObjects:atIndexes:.

[oldArray insertObjects:newArray atIndexes:indexes];

<小时>

或者,有这种方法:


Alternatively, there's this approach:

oldArray = [[newArray arrayByAddingObjectsFromArray:oldArray] mutableCopy];

这篇关于NSMutableArray - 在开始时添加数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆