NSMutablearray 将对象从一个索引移动到另一个索引

发布于 2024-10-06 02:59:06 字数 127 浏览 0 评论 0原文

我有一个带有可重新排序行的 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 技术交流群。

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

发布评论

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

评论(7

回心转意 2024-10-13 02:59:06
id object = [[[self.array objectAtIndex:index] retain] autorelease];
[self.array removeObjectAtIndex:index];
[self.array insertObject:object atIndex:newIndex];

就这样。处理保留计数很重要,因为数组可能是唯一引用该对象的数组。

id object = [[[self.array objectAtIndex:index] retain] autorelease];
[self.array removeObjectAtIndex:index];
[self.array insertObject:object atIndex:newIndex];

That's all. Taking care of the retain count is important, since the array might be the only one referencing the object.

江湖彼岸 2024-10-13 02:59:06

ARC 兼容类别:

NSMutableArray+Convenience.h

@interface NSMutableArray (Convenience)

- (void)moveObjectAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex;

@end

NSMutableArray+Convenience.m

@implementation NSMutableArray (Convenience)

- (void)moveObjectAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex
{
    // Optional toIndex adjustment if you think toIndex refers to the position in the array before the move (as per Richard's comment)
    if (fromIndex < toIndex) {
        toIndex--; // Optional 
    }

    id object = [self objectAtIndex:fromIndex];
    [self removeObjectAtIndex:fromIndex];
    [self insertObject:object atIndex:toIndex];
}

@end

用法:

[mutableArray moveObjectAtIndex:2 toIndex:5];

ARC compliant category:

NSMutableArray+Convenience.h

@interface NSMutableArray (Convenience)

- (void)moveObjectAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex;

@end

NSMutableArray+Convenience.m

@implementation NSMutableArray (Convenience)

- (void)moveObjectAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex
{
    // Optional toIndex adjustment if you think toIndex refers to the position in the array before the move (as per Richard's comment)
    if (fromIndex < toIndex) {
        toIndex--; // Optional 
    }

    id object = [self objectAtIndex:fromIndex];
    [self removeObjectAtIndex:fromIndex];
    [self insertObject:object atIndex:toIndex];
}

@end

Usage:

[mutableArray moveObjectAtIndex:2 toIndex:5];
谜泪 2024-10-13 02:59:06

使用 Swift 的 Array 就这么简单:

Swift 3

extension Array {
    mutating func move(at oldIndex: Int, to newIndex: Int) {
        self.insert(self.remove(at: oldIndex), at: newIndex)
    }
}

Swift 2

extension Array {
    mutating func moveItem(fromIndex oldIndex: Index, toIndex newIndex: Index) {
        insert(removeAtIndex(oldIndex), atIndex: newIndex)
    }
}

With Swift's Array it's as easy as this:

Swift 3

extension Array {
    mutating func move(at oldIndex: Int, to newIndex: Int) {
        self.insert(self.remove(at: oldIndex), at: newIndex)
    }
}

Swift 2

extension Array {
    mutating func moveItem(fromIndex oldIndex: Index, toIndex newIndex: Index) {
        insert(removeAtIndex(oldIndex), atIndex: newIndex)
    }
}
秋日私语 2024-10-13 02:59:06

如果您有一个 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.

不回头走下去 2024-10-13 02:59:06

你不能。 NSArray 是不可变的。您可以将该数组复制到 NSMutableArray (或者首先使用它)。可变版本有移动和交换其项目的方法。

You can't. NSArray is immutable. You can copy that array into an NSMutableArray (or use that in the first place). The mutable version has methods to move and exchange its items.

千柳 2024-10-13 02:59:06

我想如果我理解正确的话,你可以这样做:

- (void) tableView: (UITableView*) tableView moveRowAtIndexPath: (NSIndexPath*)fromIndexPath toIndexPath: (NSIndexPath*) toIndexPath

{
    [self.yourMutableArray moveRowAtIndex: fromIndexPath.row toIndex: toIndexPath.row]; 
    //category method on NSMutableArray to handle the move
}

然后你可以做的是使用 – insertObject:atIndex: 方法向 NSMutableArray 添加一个类别方法来处理移动。

I guess if I understand correctly, you can do:

- (void) tableView: (UITableView*) tableView moveRowAtIndexPath: (NSIndexPath*)fromIndexPath toIndexPath: (NSIndexPath*) toIndexPath

{
    [self.yourMutableArray moveRowAtIndex: fromIndexPath.row toIndex: toIndexPath.row]; 
    //category method on NSMutableArray to handle the move
}

Then what you can do is to add a category method to NSMutableArray using – insertObject:atIndex: method to handle the move.

昨迟人 2024-10-13 02:59:06

与 Tomasz 类似,但具有超出范围的错误处理

enum ArrayError: ErrorType {
    case OutOfRange
}

extension Array {
    mutating func move(fromIndex fromIndex: Int, toIndex: Int) throws {
        if toIndex >= count || toIndex < 0 {
            throw ArrayError.OutOfRange
        }
        insert(removeAtIndex(fromIndex), atIndex: toIndex)
    }
}

Similar to Tomasz but with out of range error handling

enum ArrayError: ErrorType {
    case OutOfRange
}

extension Array {
    mutating func move(fromIndex fromIndex: Int, toIndex: Int) throws {
        if toIndex >= count || toIndex < 0 {
            throw ArrayError.OutOfRange
        }
        insert(removeAtIndex(fromIndex), atIndex: toIndex)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文