重新排列UIImagearrays中的元素

我有一个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()newImages数组,以及data并能够运行该行重新排列UIImagearrays的顺序。

我试过这些,但不幸的是我无法让他们工作..

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

这应该工作重新排列任何二维数组:

 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 }