0

I am beginner in Swift and probably this is fundamental stuff but I need to get the index of the row (cell) I clicked on in the tableview.

I have TableViewController with predefined methods, but I wasn't able to find what I am looking for.

Rows (cells) are string from the string array.

3
  • Please review the documentation for UITableViewDelegate.
    – rmaddy
    Feb 21, 2019 at 21:13
  • Or look at the several duplicates on this very site.Prefer documentation though.
    – keji
    Feb 21, 2019 at 21:23
  • 2
    If you have a solution, do post it as an answer. Do not add it to your question. See Ask questions, get answers, no distractions Feb 21, 2019 at 21:44

3 Answers 3

3

Implement table view delegate then you can detect your index cell by using method tableView(_:didSelectRowAt:): https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614877-tableview

Specifically:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("your row number: \(indexPath.row)")
}
4
  • Thanks. I just want to get the index in Int form. Any ideas? Feb 21, 2019 at 21:16
  • 2
    indexPath.row The value of the row element of the index path. in case your table has one section only then you can miss indexPath.section and don't take it in account
    – Dmitry A.
    Feb 21, 2019 at 21:20
  • @MapeSVK: I updated my answer. You can get your Int index by using indexPath.row. Hope this helps.
    – qtngo
    Feb 21, 2019 at 21:54
  • @qtngo Yes, I already have it. I posted the answer. Anyway, thank you Feb 21, 2019 at 22:09
0
override func tableView(_ tableView: UITableView,
                        didSelectRowAt indexPath: IndexPath) {
    print(indexPath.row)
}
2
  • 2
    Instead of posting your own answer you should accept one of the other answers that told you how to do this.
    – rmaddy
    Feb 21, 2019 at 22:49
  • 1
    @rmaddy you should be more observant. He edited his answer from the point "Specifically" after I posted my solution which I got from the comments where one guy told me to connect two things together. He is just hunting reputation and his goal was to be checked. Thank you. Feb 22, 2019 at 2:23
-1

Your TableView contains TableViewCells. All the logic when you touch a cell can be programmed in

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

Update
indexPath.row will get you the index of the chosen tableviewcell

8
  • I need to get the index in Int form. Can you tell me, how? You didn't provide answer, just don't hunt reputation Feb 21, 2019 at 21:17
  • Sorry, read over the actual question, updated the answer. Better?
    – Ricardo
    Feb 21, 2019 at 21:22
  • No, I am getting all the indexes in the table, not the one I clicked on Feb 21, 2019 at 21:25
  • @Ricardo the method you mentioned in your answer has nothing to do with taping/selectibg a cell. It is called so that you can provide a cell to be drawn on screen.
    – Losiowaty
    Feb 21, 2019 at 21:28
  • @Losiowaty Could you, please, provide me some tip, or name of the method I can use to achieve my goal? Thank you Feb 21, 2019 at 21:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.