Get the index of a row clicked in a tableview?

I found in the AppKit doc Swift 5.2

Declaration

var clickedRow: Int { get }


Unfortunately I can't resolve it resp. I don't know how to use this info in myApp


Thanks for any help

gefa

Answered by Claude31 in 406546022

Sure there is:


func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { }


If you search in XCode doc for UITableView, you get it all.


Good you made it work. Don't forget to close the thread.

The way to use:


let row = myTableView.clickedRow


Note that's only for MacOS NSTableView, not iOS UITableView.

What is your app ? iOS ? MacOS ?


In iOS, you have usually the delegate

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


where you get the indexPath (hence the row) once you tapped on a cell.


Could you show the code where you want to use it ?

Hello Claude31,

my app is an iOS app. Unfortunately, I can't see in the delegates (AppDelegate.swift, SceneDelegate.Swift) the delegate func you propose.

Any idea why?

Thanks for your help

gefa

Ok, so you can't use clickedRow.


Your tableView is in a class, right ?


How is it declared?

1. Subclass of UIViewController, conforming to TableView protocols

class MyClass : UIViewController, UITableViewDelegate, UITableViewDataSource { }

or

2. directly as subClass of tableViewController

class MyClass : UITableViewController { }


In any case, you define in the class the delegate func, such as :


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


The only difference is that, in the second case, need to declare func as override


Don't forget to set the delegate for the tableView.


Does it answer your point ? If so, thanks to close the thread. If not, please explain what the problem is.

Hello Claude31,

I could implement your delegate proposal. Examples I got:

row selected is: [0, 1]

row selected is: [0, 4]

row selected is: [0, 6]

Presumably the first integer in these arrays refers to sections.

Thank you very much


Is there an analogous delegate to monitor when a selected row is unselected?

Thanks for your answer


gefa

Accepted Answer

Sure there is:


func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { }


If you search in XCode doc for UITableView, you get it all.


Good you made it work. Don't forget to close the thread.

is there a way to fetch the row by selecting on a UISegmentcontrol placed inside the cell?

Get the index of a row clicked in a tableview?
 
 
Q