Nsmutablearray 在索引处插入对象但向下移动已插入的对象

发布于 2025-01-04 18:18:44 字数 309 浏览 1 评论 0原文

我想问以下情况是否有可能发生。 我有一个 Nsmutablearray,里面有 5 个对象。我将 Nsmutablearray 加载到 uitableview 上。我想在其他五个对象的顶部插入一个新对象,但也将所有先前插入的对象向下移动一个。让我给你一个例子,

0 one
1 two
2 three
3 four
4 five

我想在将新对象插入顶部后得到这样的结果

0 six
1 one
2 two
3 three
4 four
5 five

任何帮助表示赞赏。

I wanted to ask if the following scenario is somehow possible to happen.
I have an Nsmutablearray and i have in it 5 objects. I load the Nsmutablearray on an uitableview. I want to insert a new object at the top of the other five objects but also move down all the previously inserted objects down by one. let me give you an example

0 one
1 two
2 three
3 four
4 five

i want to result like this after the new object inserted on the top

0 six
1 one
2 two
3 three
4 four
5 five

Any help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残月升风 2025-01-11 18:18:44
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index

NSMutableArray 的这个方法就是您正在寻找的。

如果索引已被占用,则通过在索引中添加 1 来移动索引处及索引之外的对象以腾出空间。

- (void)insertObject:(id)anObject atIndex:(NSUInteger)index

This method of NSMutableArray is what you are looking for.

If index is already occupied, the objects at index and beyond are shifted by adding 1 to their indices to make room.

情何以堪。 2025-01-11 18:18:44

您正在寻找的方法是:- [NSMutableArray insertObject:atIndex:]

在您的示例中,索引将为 0。

The method you are looking for is: - [NSMutableArray insertObject:atIndex:].

In your example, the index would be 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文