2

如果调用函数“Savedata”,我将新按钮添加到 [UIbutton] 中,并将新元素添加到数组 [[Double]] 中。我希望索引 [i] 上的每个按钮在索引 [i] 上的数组 [[Double]] 中显示数据。我该怎么做循环?

 @IBAction func Savedata(_ sender: Any) {

    subjectsznamky.insert(arrayx, at: 0) //subjectsznamky is the [[Double]] array

var button : UIButton
            button = UIButton(type: .system) as UIButton
            button.frame = CGRect(x:5, y: 20, width: 100.0, height: 30)
            button.setTitle(ourname, for: .normal)
            self.view.addSubview(button)
            buttons.append(button)

   for i in buttons.indices {
                buttons[i].frame.origin.y += 30
                buttons[i].addTarget // here I need to create the function, that every button on index [i] displays data in subjectsznamky on index[i]


}

谢谢你。

4

1 回答 1

4

这可能不是管理视图或在应用程序中显示数据的理想方式。您应该考虑UITableView改为。

话虽如此...

也许你可以尝试这样的事情,在字典而不是单独的数组中跟踪你的按钮和值。如果您想保持顺序,您仍然需要一个专门用于按钮的数组。

var hashes = [UIButton : [Double]]()
var buttons = [UIButton]()

@IBAction func saveData(_ sender: Any) {

    var button = UIButton(type: .system)
    button.frame = CGRect(x:5, y: 20, width: 100.0, height: 30)
    button.setTitle(ourname, for: .normal)
    self.view.addSubview(button)
    buttons.append(button)

    hashes[button] = arrayx

    for button in buttons {
        button.frame.origin.y += 30
        button.addTarget(self, action: #selector(MyClass.disaplayData(_:)), for: .touchUpInside)
    }
}

func displayData(_sender: UIButton) {
    if let doubleArray = hashes[sender] {
        print(doubleArray)
    }
}
于 2017-01-20T20:10:33.367 回答