Selecting row in a tableview

My environment is as follows:

  • Xcode 11/12
  • swift 5
  • macOS app
    I want to select the row when the right click mouse button is clicked.

I have the following code with in a right click action function

@objc   NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:row]
        tableView.selectRowIndexes(indexSet: IndexSet, byExtendingSelection:NO)

The first line with "@objc" gets "expected expression error". What am I doing wrong with this code?

@objc doesn’t allow you to insert Objective-C into your Swift source code. Rather, it’s an attribute that makes declarations visible to the Objective-C runtime.

If you want to create an index set in Swift, you’ll need Swift code for that. For example:

let indexSet = IndexSet(integer: row)

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

Thanks for the reply.. So, I changed it to the following:

let indexSet = IndexSet(integer: row)
tableView.selectRowIndexes(indexSet, byExtendingSelection: false)

No compile errors, but it still does not select the row. Can you help with this as well?

Can you help with this as well?

Sorry, no idea.

This is more of an AppKit question rather than a Swift question, so you might want to ask it over on DevForums, making sure to tag it with AppKit.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

1 Like
Terms of Service

Privacy Policy

Cookie Policy