【问题标题】:Swift: How to add items to an array using a UISwitch?Swift:如何使用 UISwitch 将项目添加到数组中?
【发布时间】:2020-09-28 02:21:01
【问题描述】:

我创建了一个表格,其中包含左侧带有文本的单元格和右侧的 UISwitch。该表链接到一个包含大约 70 个不同字符串的数组(因此该表中有 70 个单元格)。我还创建了一个要在其中存储字符串的空数组。我的问题是:如何使用 UISwitch 将字符串添加到空数组中?我似乎无法弄清楚如何引用表格中的单元格并将单元格内的任何文本添加到我的空数组中,仅使用 UISwitch。

''' func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    let utilities = Utilities()

    return utilities.allFactions.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let utilities = Utilities()

    // Writes a new faction on every line of the table
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = utilities.allFactions[indexPath.row]

    // Creates a switch that can be toggle "on" or "off"
    let mySwitch = UISwitch()
    mySwitch.addTarget(self, action: #selector(didChangeSwitch(_:)), for: .valueChanged)
    mySwitch.isOn = false
    // Adds the switch to the right side of the cell
    cell.accessoryView = mySwitch

    return cell

}

'''

【问题讨论】:

  • 您能否提供有关如何填充这些单元格的代码?您是否使用开关从新数组中添加/删除字符串?
  • 我在编辑后的帖子中包含了代码(但不知道为什么它会切断它的第一部分)。是的,这正是我想要做的。我想使用已创建的数组中的字符串将字符串附加/删除到新数组。
  • 首先 - 您正在创建两个实用程序实例。我会说把它们拿出来放在课堂上,以便于访问。其次,我会说为每个开关添加一个标签,并将 indexpath.row 作为值。这样,在您的 @objc 方法中,您可以获取正在交互的单元格并附加它。
  • 我修好了第一部分,但我不太清楚你说的第二部分是什么意思(我最近开始编程)。
  • 哦,对不起 - 所以只做 mySwitch.tag = indexPath.row (假设你只有 1 个部分)。并且,在 didChangeSwitch 方法中,确保将 sender: UISwitch 作为参数传递,并检查发送者的标签。现在由于标签的值是 indexpath.row,所以只需通过 utility.allfactions[mySwitch.tag] 访问它,您应该会得到要附加的字符串的值。

标签: arrays swift uiswitch


【解决方案1】:

这应该可以帮助您启动它。您应该进行更多检查,例如该字符串是否存在于新数组中。

// This is your datasource, your array that provides data to the tableview.
let stringSource = ["String 1", "String 2", "String 3", "String 4"]

// Initialise a new string array
var newStringArray = Array<String>()

下面是tableview的普通数据源方法

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return stringSource.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)

        cell.textLabel?.text = stringSource[indexPath.row]

        let mySwitch = UISwitch()
// Assign the index row of the cell to the tag - this ensure that the switch has a unique value to it (an ID if you may).
        mySwitch.tag = indexPath.row
        mySwitch.addTarget(self, action: #selector(didChangeSwitch(_:)), for: .valueChanged)
        mySwitch.isOn = false
        cell.accessoryView = mySwitch

        return cell
    }

和选择器方法:

    @objc func didChangeSwitch(_ sender: UISwitch) {
// Since we know where the switch comes from (via the tag property) we can 
// simply access the value and append it to our new one
        newStringArray.append(stringSource[sender.tag])
        print(newStringArray)

    }

【讨论】:

  • 很高兴它有效!如果它解决了您的问题 - 请不要忘记将此答案设为正确,以便将来其他人也可以找到此解决方案。
  • 当然!我也是这个网站的新手。我该怎么做?
  • 只需勾选此解决方案旁边的向上箭头
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-01
  • 1970-01-01
  • 2018-12-11
  • 2013-06-05
  • 1970-01-01
  • 2011-07-01
  • 1970-01-01
相关资源
最近更新 更多