Search code examples
swifttableviewcell

Set String Identifier to Cell in Swift


I have a TableView that receives an array of string identifiers, what i need to do is to set an identifier for every cell that i have and associate it to a variable called "cellPressedId", but when i do that, i just obtain the last value of the array

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

What I need to do is to set this identifier to every cell and when i pressed this cell obtain the identifier


Solution

  • when a cell is selected you have access to the indexPath of the selected cell via this function: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) and I'm sure you have access to your array of data (array of strings you mentioned that) you can access selected elements like this :

    let selectedItem = myArray[indexPath.row]