I have a cell-based NSTableView that includes a checkbox in the first column. Everything displays and seems to function correctly, except the handling of the checkbox action, which looked like so:
- (IBAction)ActiveCheckboxAction:(id)sender {
// Do stuff with the checkbox
}
I was surprised to discover that when ActiveCheckboxAction is called, sender points to the table view; not the checkbox. To get around this problem I was looking for a way to access the checkbox from the currently selected row, so I modified the code, like so:
- (IBAction)ActiveCheckboxAction:(id)sender {
NSTableCellView *cellView = [tableView viewAtColumn:0 row:[tableView selectedRow] makeIfNecessary:NO];
NSButton *checkBox = [[cellView subviews] objectAtIndex:0];
// Do stuff with the checkbox
}
Here, cellView is always nil, so I'm guessing that viewAtColumn applies to only view-based table views. In examining the table view I can see the checkboxes in the _datacell for the first column correctly described as an (NSButton *), but I can't figure out how to access this cell to get to the checkbox.
Two questions:
Why would sender be pointing to the table view?
What can I do to get the checkbox from the selected row?
Thanks!
Why would sender be pointing to the table view?
The sender is the control, in this case the table view. NSTableView is a subclass of NSControl.
What can I do to get the checkbox from the selected row?
The checkbox cell is [sender selectedCell] in the action method.
Instead of the action method you can use tableView:setObjectValue:forTableColumn:row:. The object value is the state of the checkbox as NSNumber.
Related
I have a custom UITableViewCell and a custom UITextField, with the text field inside the table view cell.
I need to do some validation on text entered in the field, and display a little tick or cross image in the cell depending on if the text entered was okay.
My table view cell class imports the text field's header, and declares a .textField property which I can use.
My text field class has a class extension (#class) of my table view cell, and declares a .cell property, which I'd like to be able to use to access the cell's image, and display / update it to the tick or cross.
The text field's delegate is my view controller, and I'm using textFieldDidEndEditing in the following way to attempt to set that image (validationConfirmation). However, it's not working, nothing's happening, no error, etc.
Any idea what I'm doing wrong? This is a puzzler.
- (void)textFieldDidEndEditing:(UITextField *)textField
{
SignUpTextField *tf = (SignUpTextField *)textField;
if (textField.text.length < 6) {
tf.cell.validationConfirmation.hidden = NO;
tf.cell.validationConfirmation.image = [UIImage imageNamed:#"cross.png"];
}
}
So here, I'm getting the text field subclass (SignUpTextField, or I'm attempting to), and trying to set its cell property's image property. Is this wrong? Messy? Bad practise?
You don't need to (and you shouldn't) have a cell reference inside textField class. I believe your textField is added as a subview of the cell. Hence in the textFieldDidEndEditing: method, you can get the cell using textField.superview. Or alternately, you can assign a tag to the textField corresponding to the tableview row that it appears in and access the cell using cellForRowAtIndexPath: method of tableview where you can use the tag to create your indexpath.
I have a view based table view with one row containing NSPopupButtons. When the user changes the popUpButton I need to get get the table row in which this popUpButton is contained.
I first expected to get the row with
NSInteger clickedRow = [tableView rowForView: ((NSPopUpButton*) sender)];
But the sender of the action always is a NSMenuItem object not the NSPopUpButton. NSMenuItem however is not a view so I cannot use rowForView with that.
Currently my IBAction looks like this:
- (IBAction)changedPopUp:(id)sender {
NSMenuItem* selectedMenuItem = ((NSMenuItem*) sender);
NSPopUpButton* popupButton = (NSPopUpButton*)[selectedMenuItem view];
NSInteger clickedRow = [tableView rowForView:popupButton];
//[...]
}
But the view property is not set automatically and I find it quite inconvenient to set it manually for every NSMenuItem.
Is there no easy way to get the table row?
Don't set an action and target for each NSMenuItem. Set the action and target for the NSPopupButton instead. Then, sender will be the NSPopupButton instance and you can use rowForView.
The drawback is of course that you cannot have different actions for different menu items.
I have a NSCollectionView and I am adding my custom view which acts as NSCollectionViewItem for that collection view. In my collectionViewItem, I have a NSButton along with various other elements. I have a method onButtonClick which is connected to that button. Now, suppose I add 5 items of my collectonViewItem on to the collectionView.
How can I get the index of the view from where the button was clicked?
Inside onButtonClick, I tried following code but it always returns 0 regardless of which button I click:
id collectionViewItem = [sender superView];
NSInteger index = [[colloectionView subviews] indexOfObject:collectionViewItem];
What is the right way to achieve this?
you cant return index by clicking a control (which having its own functon).i think you have to see http://andrehoffmann.wordpress.com/2009/08/29/nscollectionview-tutorial-for-dummies-xcode-3-1-3/.
before that check collectionview selection as selectable in attributes inspector.(and also try by disable the button)
I use a view-based NSOutlineView to display and select hierarchically structured items for a scientific application.
Each row in the outline column represents an item, signified by an item-specific icon (all the same in the picture), a checkbox that shows if the item is selected, and the name of the item. I need the icon, the checkbox and the name to appear in the same cell, hence I am using a view-based NSOutlineView.
I have implemented the NSOutlineViewDataSource protocol to supply data to the the outline view.
The method outlineView:objectValueForTableColumn:byItem: supplies a custom object that has the properties BOOL selected and NSString *name.
My custom table cell view in IB is composed as follows:
I bound the check box value to objectValue.selected and the label value to objectValue.name.
As I hoped, the outline view displays nicely the name and selection state supplied by the objectValue.
However, if I change the state of the check box, the method outlineView:setObjectValue:forTableColumn:byItem: that is defined in the NSOutlineViewDataSource protocol is not triggered in my dataSource to supply the newly changed object value. Note that if I don't use a custom view for the cell this works.
I checked whether the table cell view's objectValue.selected actually gets changed when clicking the check box by inserting an NSLog statement into the setSelected method of the object that is passed as objectValue. The selected member changes state correctly.
How do I propagate the change of the objectValue back to my dataSource's model? I have checked the NSOutlineView's delegate methods, but can't find a way to signal that the cell view's objectValue was changed by my check box (i.e., that the cell view has "ended editing"). Is there some other fundamental point I am missing?
setObjectValue doesnt work for view based ones:
from header::
/* View Based OutlineView: This method is not applicable.
*/
- (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item;
I was able to solve this problem by creating a subclass of NSTableCellView, making it the delegate of the contained NXTextField, and using the edited value to update NSTableCellView's object value.
class CategoryNameTableViewCell : NSTableCellView, NSTextFieldDelegate {
func control(_ control: NSControl, textShouldEndEditing fieldEditor: NSText) -> Bool {
guard var category = self.objectValue as! Category? else {
Swift.print("Tried to edit category cell with no object!")
return false
}
category.name = control.stringValue
category.saveChanges()
return true
}
}
I have a small NSTableView with a checkbox. Whenever the checkbox is not checked, I want one of the adjacent NSCells to be grayed out and inaccessible.
However, I can't figure out how to address only one specific cell. -dataCellForRow of NSTableColumn always changes the template cell for the whole table column.
How can I access one single cell?
Edit: I fill the table view using the NSTableViewDataSource protocol.
You don't "access a cell". NSTableView asks for data only when necessary, you don't populate it or control it directly.
Instead, you create a controller object which implements the NSTableViewDatasource and optionally NSTableViewDelegate protocols. The table view then sends the datasource messages to your controller and your controller supplies the appropriate data.
You can allow editing for an object displayed in the table view by implementing the ‑tableView:setObjectValue:forTableColumn:row: datasource method. This method will be called on your controller object when the user clicks the checkbox. It is your controller's responsibility to update the model appropriately.
When the model is updated, your controller should tell the table view to reload. The table view will then ask your controller for the value of any cell that requires display using the ‑tableView:objectValueForTableColumn:row: datasource method. This will include the cell that you need to disable. Your controller needs to supply the appropriate value for the cell.
If you need more control of the cell, you can implement the
‑tableView:willDisplayCell:forTableColumn:row: delegate method. This is called just before a cell is displayed, and you can modify the cell appropriately.
More info about using data sources is in the docs.
The other option (instead of using a datasource) is to use Cocoa Bindings and an NSArrayController that you bind to your collection of model objects. In that case, you can bind the Enabled binding of the table column to some property of your model object that controls the cell's enabled state. It is your responsibility to ensure that the state of that property is correct.
If you need to make the property dependent on the value of another property, you can use the dependent key mechanism outlined in the Key-Value Observing documentation.
Could you bind the editability of that column to the value that is being displayed in the checkbox? i.e. if it is checked, it is editable, otherwise it isn't?
I am trying to remember the exact editor interface, and I am not next to my Mac at home, so I am not able to do a total walk through on it - hope this can point you in the right direction.
Since SDK Version 10.7, there's -viewAtColumn:row:makeIfNecessary: on NSTableView. The majority of information I found on the web don't take the new methods into account, so here it is for all the others looking for an answer to this question.
From Mouse Event to Cell Selection
First, add a protocol for your controller to handle cell selection from a table view, like this:
#protocol XYZCellSelectionDelegate <NSObject>
- (void)cellViewWasSelectedAtRow:(NSInteger)row column:(NSInteger)column;
#end
Then subclass NSTableView and override -mouseDown:
// In your Custom Table View subclass:
- (void)mouseDown:(NSEvent *)event
{
NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
NSInteger selectedRowIndex = [self rowAtPoint:point];
NSInteger selectedColumnIndex = [self columnAtPoint:point];
if ([self.calendarViewDelegate respondsToSelector:#selector(cellViewWasSelectedAtRow:column:)])
{
[self.calendarViewDelegate cellViewWasSelectedAtRow:selectedRowIndex column:selectedColumnIndex];
}
[super mouseDown:event];
}
Afterwards, you can use -viewAtColumn:row:makeIfNecessary: like this in the delegate/controller object:
- (void)cellViewWasSelectedAtRow:(NSInteger)row column:(NSInteger)column
{
NSView *selectedView = [self.tableView viewAtColumn:column row:row makeIfNecessary:YES];
// Do something with the cell to the right
NSInteger nextColumn = column + 1;
NSView *cellNextToIt = [self.calendarTableView viewAtColumn:nextColumn row:row makeIfNecessary:YES];
}
Note: Nowadays, I'd pass the table view to the delegate as a parameter instead of relying on the delegate to keep a reference to the table view.