NSMutablearray 将对象从一个索引移动到另一个索引
我有一个带有可重新排序行的 UItableview,数据位于 NSarray 中。那么,当调用适当的表视图委托时,如何移动 NSMutablearray 中的对象呢?
另一种问法是如何重新排序 NSMutableArray?
I have a UItableview with reordable rows and the data is in an NSarray. So how do I move an object in the NSMutablearray when the appropriate tableview delegate is called?
Another way to ask this is how to reorder an NSMutableArray?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
就这样。处理保留计数很重要,因为数组可能是唯一引用该对象的数组。
That's all. Taking care of the retain count is important, since the array might be the only one referencing the object.
ARC 兼容类别:
NSMutableArray+Convenience.h
NSMutableArray+Convenience.m
用法:
ARC compliant category:
NSMutableArray+Convenience.h
NSMutableArray+Convenience.m
Usage:
使用 Swift 的
Array
就这么简单:Swift 3
Swift 2
With Swift's
Array
it's as easy as this:Swift 3
Swift 2
如果您有一个 NSArray,则无法移动或重新排序任何内容,因为它是不可变的。
您需要一个 NSMutableArray。这样,您就可以添加和替换对象,当然,这也意味着您可以对数组进行重新排序。
If you have an
NSArray
, you can't move or reorder anything as it is immutable.You need an
NSMutableArray
. With that, you can add and replace objects which, of course, also means you can reorder the array.你不能。
NSArray
是不可变的。您可以将该数组复制到NSMutableArray
(或者首先使用它)。可变版本有移动和交换其项目的方法。You can't.
NSArray
is immutable. You can copy that array into anNSMutableArray
(or use that in the first place). The mutable version has methods to move and exchange its items.我想如果我理解正确的话,你可以这样做:
然后你可以做的是使用 – insertObject:atIndex: 方法向 NSMutableArray 添加一个类别方法来处理移动。
I guess if I understand correctly, you can do:
Then what you can do is to add a category method to NSMutableArray using – insertObject:atIndex: method to handle the move.
与 Tomasz 类似,但具有超出范围的错误处理
Similar to Tomasz but with out of range error handling