Print the NSTableView's row number of the row clicked by the user
You can use the selectedRowIndexes
property from the tableView in the tableViewSelectionDidChange
method in your NSTableView delegate.
In this example, the tableView allows multiple selection.
Swift 3
func tableViewSelectionDidChange(_ notification: Notification) {
if let myTable = notification.object as? NSTableView {
// we create an [Int] array from the index set
let selected = myTable.selectedRowIndexes.map { Int($0) }
print(selected)
}
}
Swift 2
func tableViewSelectionDidChange(notification: NSNotification) {
var mySelectedRows = [Int]()
let myTableViewFromNotification = notification.object as! NSTableView
let indexes = myTableViewFromNotification.selectedRowIndexes
// we iterate over the indexes using `.indexGreaterThanIndex`
var index = indexes.firstIndex
while index != NSNotFound {
mySelectedRows.append(index)
index = indexes.indexGreaterThanIndex(index)
}
print(mySelectedRows)
}
Get button's row in view based table
-[NSTableView rowForView:]
says this in its documentation:
This is typically needed in the action method for an NSButton (or NSControl) to find out what row (and column) the action should be performed on.
NSTableView Right Clicked Row Index
While I haven't done this, I am pretty sure you can by overriding NSView
's - (NSMenu*)menuForEvent:(NSEvent*)theEvent
. The example in this link does a point conversion to determine the index.
-(NSMenu*)menuForEvent:(NSEvent*)theEvent
{
NSPoint mousePoint = [self convertPoint:[theEvent locationInWindow] fromView:nil];
int row = [self rowAtPoint:mousePoint];
// Produce the menu here or perform an action like selection of the row.
}
NSTableView: detecting a mouse click together with the row and column
To catch the user clicking a row (only, when the user clicks a row, not when it is selected programmatically) :
Subclass your NSTableView and declare a protocol
MyTableView.h
@protocol ExtendedTableViewDelegate <NSObject>
- (void)tableView:(NSTableView *)tableView didClickedRow:(NSInteger)row;
@end
@interface MyTableView : NSTableView
@property (nonatomic, weak) id<ExtendedTableViewDelegate> extendedDelegate;
@end
MyTableView.m
Handle the mouse down event (note, the delegate callback is not called when the user clicks outside, maybe you want to handle that too, in that case, just comment out the condition "if (clickedRow != -1)
")
- (void)mouseDown:(NSEvent *)theEvent {
NSPoint globalLocation = [theEvent locationInWindow];
NSPoint localLocation = [self convertPoint:globalLocation fromView:nil];
NSInteger clickedRow = [self rowAtPoint:localLocation];
[super mouseDown:theEvent];
if (clickedRow != -1) {
[self.extendedDelegate tableView:self didClickedRow:clickedRow];
}
}
Make your WC, VC conform to ExtendedTableViewDelegate.
@interface MyViewController : DocumentBaseViewController<ExtendedTableViewDelegate, NSTableViewDelegate, NSTableViewDataSource>
set the extendedDelegate of the MyTableView to your WC, VC (MyViewController)
somewhere in MyTableView.m
self.myTableView.extendedDelegate = self
Implement the callback in delegate (MyViewController.m)
- (void)tableView:(NSTableView *)tableView didClickedRow:(NSInteger)row {
// have fun
}
detecting clicked tableView
Well, on osx in the current AppKit (8.2.1) state on Sierra this is not doable.
My gui is using a single action control for a set of header/detail tableViews and I needed to know which was in effect, so I'm to mandating an empty selection in the detail table view to indicate the header view selection is to be used.
But I would like to know why osx swift doesn't have a comparable delegate method set that ios or objective-c have?
get value of selected row NSTableView
Cocoa follows the Model-View-Controller design pattern, so your reasoning as well as your own designs should as well.
You don't get any values from the table because you already have them in your model. Your controller (usually the data source) asks the table for its selected row index(es), then asks your model for the objects matching those indexes, then does whatever it needs to do.
Related Topics
Swiftui: How to Make a Button Open a Url in Safari
Swift Task Continuation Misuse: Leaked Its Continuation - for Delegate
Swift Maximum Consecutive Positive Numbers
Swift 2 to 3 Migration Dispatch_Get_Global_Queue
Swift [1,2] Conforms to Anyobject But [Enum.A, Enum.B] Does Not
Hot to Decode JSON Data That Could and Array or a Single Element in Swift
An Issue with Cashapelayer and Uibezierpath Drawing Circle Counterclockwise
Xcode & Swift - Window Without Title Bar But with Close, Minimize and Resize Buttons
Swift Convert Time to Time Ago
How to Identify Uppercase and Lowercase Characters in a String with Swift
How to Open a Screen Directly in Xcuitest
Why Do Integers Not Conform to the Anyobject Protocol
Nsdatecomponents Weekofyear Returns Wrong Date
How Should You Handle Closure Arguments for Uialertaction