重新排序里面的UIImage的数组元素 [英] Reordering Elements inside Array of UIImage

查看:126
本文介绍了重新排序里面的UIImage的数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有NSURL数组的数组,我能够使用的功能 removeAtIndex 插入。我知道 fromIndexPath toIndexPath 键,这种方法可以帮助我来完成使用该委托[NSURL]]相同方法(检查 VAR数据以下):

  FUNC moveDataItem(fromIndexPath:NSIndexPath,toIndexPath:NSIndexPath){
      让名= self.data这样[fromIndexPath.section] [fromIndexPath.item]
      self.data这样[fromIndexPath.section] .removeAtIndex(fromIndexPath.item)
      self.data这样[toIndexPath.section] .insert(姓名,atIndex:toIndexPath.item)     //做同样的UIImage的阵列
}

不过,我的UIImage与3个空元素的数组与运行。

  VAR newImages = [UIImage的?() viewDidLoad中(){
    newImages.append(无)
    newImages.append(无)
    newImages.append(无)
 }

我的问题是,我怎么可以使用里面的 newImages 阵列 moveDataItem() ,还有数据,并能够运行为重新排列顺序的UIImage阵列线。

我想这些只可惜我不能让他们的工作。

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


有关澄清,该数据阵列看起来像这样

 懒VAR数据:[[NSURL] = {    变种数组= [[NSURL]()
    让图像= self.imageURLsArray    如果array.count == 0 {        VAR指数= 0
        VAR部分= 0
        在图像的图像{
            如果array.count< = {节
                array.append([NSURL]())
            }
            数组[第] .append(图)            指数+ = 1
        }
    }
    返回的数组
}()


解决方案

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

  FUNC举动< T>(fromIndexPath:NSIndexPath,toIndexPath:NSIndexPath,项目:[[T]]) -  GT; [[T]] {    VAR newData =项目    如果newData.count> 1 {
        让事情= newData [fromIndexPath.section] [fromIndexPath.item]
        newData [fromIndexPath.section] .removeAtIndex(fromIndexPath.item)
        newData [toIndexPath.section] .insert(事,atIndex:toIndexPath.item)
    }
    返回newData
}

使用例子:

  VAR东西= [[喜,有],[家伙,女生]]//[[喜,有],[家伙,女生]]的\\ n
打印(的东西)事情=移动(NSIndexPath(forRow:0,切入口:0),toIndexPath:NSIndexPath(forRow:1,切入口:0),项目:东西)//[还有,喜],[家伙,女生]]的\\ n
打印(的东西)

,这将与正常工作数组:

  FUNC举动< T>(的fromIndex:智力,元素范围为:智力,项目:[T]) -  GT; [T] {    VAR newData =项目    如果newData.count> 1 {
        让事情= newData [的fromIndex]
        newData.removeAtIndex(的fromIndex)
        newData.insert(事,atIndex:元素范围)
    }
    返回newData
}

I have an array of NSURL array, and I am able to use functions removeAtIndex and insert. I know the fromIndexPath and toIndexPath and this method helps me to accomplish the same for [[NSURL]] using this Delegate method (check var data below):

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
}

However, I have an array of UIImage with 3 empty elements with running.

var newImages = [UIImage?]()

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

My question is how can I use the newImages array inside moveDataItem(), as well as the data and be able to run that lines for rearranging the order for UIImage array.

I tried these but unfortunately I couldn't make them work..

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


For clarification, the data array looks like this

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

解决方案

This should work for rearranging any 2d 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
}

example usage:

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)

And this will work with a normal array:

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
}

这篇关于重新排序里面的UIImage的数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆