从数组1中删除索引并将其插入到数组2中

时间:2016-02-28 21:06:18

标签: ios arrays swift insert uibutton

如何从数组1中删除索引并将其插入数组2?数组1和数组2是[UIButton]。

例如:如何成为

@IBOutlet var Array1: [UIButton]!
@IBAction func ArrayToRemove(sender: UIButton!) {
                    sender.hidden = true 
                    }

3 个答案:

答案 0 :(得分:1)

也许是这样的?

var list0 = [
    UIButton(type: UIButtonType.ContactAdd),
    UIButton(type: UIButtonType.Custom),
    UIButton(type: UIButtonType.DetailDisclosure)
]

var list1 = [
    UIButton(type: UIButtonType.InfoDark),
    UIButton(type: UIButtonType.InfoLight)
]

let index = 1

let elm = list0.removeAtIndex(index)
list1.insert(elm, atIndex: index)

结果

list0 // [1, 5, 7]
list1 // [2, 3, 4, 6]

答案 1 :(得分:0)

如果你不知道要移动/移除的对象的索引(在数组中),但是你知道它的值,那么使用这种方法:

    let a = UIBarButtonItem(); let b = UIBarButtonItem(); let c = UIBarButtonItem()
    var array1 = [a,b,c]

    //look for the index of c in array1
    let indexOfButton = array1.indexOf(c)

    //assign the object at that index to a property so you can add it to array2
    let buttonToMove = array1[indexOfButton!]
    var array2 = [buttonToMove]

    //if you need to remove the object from the first array and place it in the second you can use the removeAtIndex method
     let buttonToMove = array1.removeAtIndex(indexOfButton!)
     var array2 = [buttonToMove]

答案 2 :(得分:-1)

您可以使用removeAtIndex功能,例如:

let removedButton = buttonsArray.removeAtIndex(index)