【问题标题】:NSMutablearray move object from index to indexNSMutablearray 将对象从索引移动到索引
【发布时间】:2023-04-05 18:30:01
【问题描述】:

我有一个带有可重新排序行的 UItableview,并且数据在一个 NSarray 中。那么当调用相应的 tableview 委托时,如何在 NSMutablearray 中移动对象呢?

问这个问题的另一种方法是如何重新排序 NSMutableArray?

【问题讨论】:

标签: iphone objective-c nsarray


【解决方案1】:

使用 Swift 的 Array 就这么简单:

斯威夫特 3

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

斯威夫特 2

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

【讨论】:

    【解决方案2】:

    与 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)
        }
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 如果你的fromIndex小于你的toIndex,你最好减少toIndex
      • Richard 的观点是有效的,前提是您指定了一个指向移动前数组中位置的 toIndex。如果这是您的期望,那么您可能应该按照建议减少 toIndex。但是,如果您指定的 toIndex 是指它在数组中的最终位置,那么您不应该递减。
      • @OliverPearmain 抱歉,我很难理解这个想法。在调用moveObjectAtIndex:0 toIndex:1 之后使用您的方法(使用建议的减量)数组[A,B,C] 将导致[A,B,C]。这合乎逻辑吗?
      • remove 有效地减少了fromIndex 之后所有对象的索引。
      【解决方案4】:
      id object = [[[self.array objectAtIndex:index] retain] autorelease];
      [self.array removeObjectAtIndex:index];
      [self.array insertObject:object atIndex:newIndex];
      

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

      【讨论】:

      • 当您保留指向该事物的指针时,没有太多理由自动释放,是吗?而不是在删除之前保留,在插入之后释放。
      • 是的,这是真的。只是它减少了一行代码,但是我确实理解您的观点,即直接发布它会更便宜。我真的不介意一个对象,但是你最好在一个(紧密的)循环中避免这种情况。
      • 为什么不使用 - (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;
      • @SarenInden 因为该方法交换指定的对象,而不是将单个对象移动到不同的位置。
      【解决方案5】:

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

      - (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
      }
      

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

      【讨论】:

      • 是的,我正在尝试查找要实现的类别方法的代码,它应该是插入然后删除吗?
      【解决方案6】:

      如果您有 NSArray,则不能移动或重新排序任何内容,因为它是不可变的。

      您需要NSMutableArray。有了它,您可以添加和替换对象,当然,这也意味着您可以重新排列数组。

      【讨论】:

        【解决方案7】:

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

        【讨论】:

        • 对不起,我的意思是可变的,但是 nsmutablearray 没有移动方法。
        • 当然有。试试-exchangeObjectAtIndex:withObjectAtIndex:。还有各种排序方法。
        • 不切换2个对象,我要移动1个。
        • 正如其他发帖者所指出的,您需要删除并重新添加。
        猜你喜欢
        • 1970-01-01
        • 2011-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-16
        • 1970-01-01
        相关资源
        最近更新 更多