将字符串标识符设置为 Swift 中的单元格

Set String Identifier to Cell in Swift

我有一个接收字符串标识符数组的 TableView,我需要做的是为我拥有的每个单元格设置一个标识符并将其关联到一个名为“cellPressedId”的变量,但是当我这样做时,我只是获取数组的最后一个值

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let selectedCell = tableView.cellForRow(at: indexPath) {
            if let selectedCell = tableView.cellForRow(at: indexPath) {
                var cellPressed = self.cellPressedId
            }
        }

我需要做的是为每个单元格设置这个标识符,当我按下这个单元格时获取标识符

选中单元格后,您可以通过此函数访问所选单元格的 indexPath: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 而且我确定您可以访问您的数据数组(您提到的字符串数组),您可以像这样访问选定的元素:

let selectedItem = myArray[indexPath.row]