【问题标题】:Reordering Elements inside Array of UIImage重新排序 UIImage 数组中的元素
【发布时间】:2016-04-21 00:24:42
【问题描述】:

我有一个 NSURL 数组,我可以使用函数 removeAtIndexinsert。我知道fromIndexPathtoIndexPath,这个方法可以帮助我使用这个Delegate 方法为[[NSURL]] 完成同样的事情(检查下面的var data):

func moveDataItem(fromIndexPath : NSIndexPath, toIndexPath: NSIndexPath) {
      let name = self.data[fromIndexPath.section][fromIndexPath.item]
      self.data[fromIndexPath.section].removeAtIndex(fromIndexPath.item)
      self.data[toIndexPath.section].insert(name, atIndex: toIndexPath.item)

     // do same for UIImage array
}

但是,我有一个 UIImage 数组,其中包含 3 个正在运行的空元素。

var newImages = [UIImage?]()

 viewDidLoad() {
    newImages.append(nil)
    newImages.append(nil)
    newImages.append(nil)
 }

我的问题是如何在moveDataItem()data 中使用newImages 数组,并能够运行这些行来重新排列UIImage 数组的顺序。

我尝试了这些,但不幸的是我无法让它们工作..

self.newImages[fromIndexPath.section].removeAtIndex(fromIndexPath.item)
// and
self.newImages[fromIndexPath.row].removeAtIndex(fromIndexPath.item)

为了澄清,数据数组看起来像这样

lazy var data : [[NSURL]] = {

    var array = [[NSURL]]()
    let images = self.imageURLsArray

    if array.count == 0 {

        var index = 0
        var section = 0


        for image in images {
            if array.count <= section {
                array.append([NSURL]())
            }
            array[section].append(image)

            index += 1
        }
    }
    return array
}()

【问题讨论】:

  • 所以您想要一种通常围绕二维数组移动的方法?
  • 是的,我想在moveDataItem() 中为data 做同样的事情,也为UIImage array 做同样的事情。

标签: ios arrays swift nsindexpath


【解决方案1】:

这应该适用于重新排列任何二维数组:

func move<T>(fromIndexPath : NSIndexPath, toIndexPath: NSIndexPath, items:[[T]]) -> [[T]] {

    var newData = items

    if newData.count > 1 {
        let thing = newData[fromIndexPath.section][fromIndexPath.item]
        newData[fromIndexPath.section].removeAtIndex(fromIndexPath.item)
        newData[toIndexPath.section].insert(thing, atIndex: toIndexPath.item)
    }
    return newData
}

示例用法:

var things = [["hi", "there"], ["guys", "gals"]]

// "[["hi", "there"], ["guys", "gals"]]\n"
print(things)

things = move(NSIndexPath(forRow: 0, inSection: 0), toIndexPath: NSIndexPath(forRow:1, inSection:  0), items: things)

// "[["there", "hi"], ["guys", "gals"]]\n"
print(things)

这适用于普通数组:

func move<T>(fromIndex : Int, toIndex: Int, items:[T]) -> [T] {

    var newData = items

    if newData.count > 1 {
        let thing = newData[fromIndex]
        newData.removeAtIndex(fromIndex)
        newData.insert(thing, atIndex: toIndex)
    }
    return newData
}

【讨论】:

  • 这是一个委托函数,一切都适用于data。问题是在moveDataItem() 中调整 UIImage 数组。我想你误会我了。 :/我编辑了我的问题以澄清更多
  • 是的,我重新阅读了您的问题并注意到了这一点。为一维数组添加了一个函数
  • 那么我应该创建另一个委托方法(你的最后一个例子)并在我调用 moveDataItem 方法时调用它吗?这是正确的方法吗?我想用新的顺序覆盖 UIImage 数组。
  • 设计模式由你决定。在不了解您的应用的情况下,我无法真正发表意见,但据我所知,您的想法应该可行
  • 突变版本作为Array 的扩展,如果你想将它添加到你的答案中(或者我可以写另一个)。 extension Array where Element : _ArrayType { mutating func move(from fromIndexPath: NSIndexPath, to toIndexPath: NSIndexPath) { let first = self[fromIndexPath.section][fromIndexPath.row] let second = self[toIndexPath.section][toIndexPath.row] self[fromIndexPath.section][fromIndexPath.row] = second self[toIndexPath.section][toIndexPath.row] = first } }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-07
  • 1970-01-01
  • 2018-08-01
  • 2016-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多