Home > Software design >  Swift Array index assigned to variable?
Swift Array index assigned to variable?

Time:11-29

I want to find the array index of the selected element in a table and assign it to a variable.

enter image description here

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
        let kategorilerVC = KategoriController()
        let kategoriIndexCode = firstList[indexPath.row]
        print(kategoriIndexCode)

image

CodePudding user response:

The array index is indexPath.row, which is the same variable you are using to access the value of the array when you do this:

firstList[indexPath.row]

So in your code, you could do this:

let kategoriIndexCode = indexPath.row
  • Related