Getting a column in an NSTableView with editable column order
-
17-06-2021 - |
سؤال
I have an NSTableView
where I would like to be notified if the user clicks in a column "ClickMe". I linked the entire table view to a method which can extract the clickedColumn:
, but I get an absolute number and not a reference to the "ClickMe" column (which may have been moved to another place).
I could of course program my own search algorithm to see if column X is actually the "Clickme" column, but that would not be very elegant. Is there a way to identify columns properly, and to receive that ID programmatically?
المحلول 2
I found a way to do my own search in a fairly fast way, but I still have a feeling I am putting too much effort in this:
First, set the Identifier of the desired column in the Interface Builder to "ClickMeColumn". Then:
NSInteger cmColumn = [tableView columnWithIdentifier:@"ClickMeColumn"];
if ( [tableView clickedColumn] == cmColumn )
NSLog(@"Clicked me!");
I am looking for something along the lines of [tableView clickedColumnIdentifier]
.
نصائح أخرى
What about querying NSTableView's columnAtPoint: in your table views mouseDown: or mouseUp: method?
Use any of the methods below. Called by the tableView's delegate on selection. You can extract the identifier and the title string from the relevant tableColumn.
- (void)tableView:(NSTableView *)tableView didClickTableColumn:(NSTableColumn *)tableColumn {
NSLog(@"tableView:didClickTableColumn: %@, titleString: %@", [tableColumn identifier], [[tableColumn headerCell] stringValue]);
}
- (void)tableView:(NSTableView *)tableView mouseDownInHeaderOfTableColumn:(NSTableColumn *)tableColumn {
NSLog(@"tableView:mouseDownInHeaderOfTableColumn: %@, titleString: %@", [tableColumn identifier], [[tableColumn headerCell] stringValue]);
}