Receive new value from edited cell in View-Based Table View - cocoa

I have an View-Based Table View with some standard NSTableCellView data rows, which I fill from my data source. User can edit text field of a cell. Please, explain me, how to receive the new value, in order to I can update my data source, when user change text in cell?

I think the best way is to have a controller object with a xib file that contains your table view (the controller could be a subclass of NSViewController). Make this controller the delegate of your table view and put an IBAction connected to your text field in this class. You should also have an outlet to the table view. Then, in your action method for the text filed, you can get the row that contains the text field from [tableView rowForView:sender]. Once you have the row, you can update the proper object in your array.

Related

How is NSTableView Content Sorted After Array Controller Add?

After adding a managed object in array controller class, the bound table view 'places' it into the correct date sorted order.
However, as the table view is building it's rows, the new object has been placed at the bottom of the array controller's content array using:
Edit: Solution: Don't use the array controller's content. The issue was in:
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
....
// originally
NSManagedObject *ci = [[self.arrayController content] objectAtIndex:row];
// should be: (note arrangedObjects replacing content)
NSManagedObject *ci = [self.arrayController.arrangedObjects objectAtIndex:row];
....
}
The image below shows an example of the placement. Console output from within -tableView: viewForTableColumn: row: at left (from original code above) and ordering in the table view on right. LHR is the new object (notice date order ascending).
Tried:
Saving the moc right after adding newObject
Calling Array Controller's arrangeObjects method
Attempting to manually setSortDescriptors
Binding the table view's selection index to the array controller (for grins)
This is also messing up the selectedRow even though the row with LHR is selected in the table view right after adding. The only way to correct the array controller's order is to manually sort the column.
How can the array controller's content be in sync with the table view? Even more, why might they not be in the same order?
The array controller "arranges" its content. It does this by calling its -arrangeObjects: method. The default implementation filters the contents using the array controller's filterPredicate and then sorts it using its sortDescriptors. A subclass could override that method to arrange the content differently.
The arranged contents can be accessed using the arrangedObjects property. This is what corresponds to rows in the table view. So, you should always use this if you're indexing by a table row. For example:
NSManagedObject *ci = [self.arrayController.arrangedObjects objectAtIndex:row];
The array controller's sortDescriptors may be set via bindings (e.g. if the table view's sortDescriptors binding is bound to the array controller). Depending on how you set up the table view bindings, this may be automatic. For NSCell-based table views, you typically bind the table columns value binding and don't bind the table views bindings. In that case, the table view automatically binds its content, selectionIndexes, and sortDescriptors bindings to the same controller as its columns. For view-based table views, you typically don't bind table columns and have to bind those table view bindings explicitly if you want them bound.
You can also set the array controller's sortDescriptors programmatically.
Try invoking rearrangeObjects on the NSArrayController and then doing the selection you need subsequent to that in a dispatch_async block. It's maddening, but some of the bindings are deferred to the next runloop turn, so you can't simply add an object, call rearrangeObjects, and select the new object in synchronous code. You have to use GCD to schedule things
See this question/answer for more discussion. It's is the exact same issue. Adding an object and trying to select it.
Enabling the Auto Rearrange Content option of the Array Controller made it for me.

Cocoa binding NSTableView within NSTableView

I'm trying to display a NSTableView inside a NSTableView. This is for an iTunes-like albums/tracks view. So there's a list of 10 albums, each album has some tracks. I want to display the albums in the outer table view and the tracks for each album in the inner.
The first NSTableView is bound to an NSArrayController. Each object in the array has its own "tracks" NSArrayController, but I can't figure out how to tell the 'tracks' NSTableView that its content comes from a property of the 'album' NSTableView.
If I understand you right, the source content of the nested array controller comes from objectValue of the owner table cell. So you can't put the array controller content source to be the objectValue of the table cell. I'm doing similar in that I want to filter the array content based on the object value
What I'm doing, which seems to be working, is to make a seperate nib file for your nested table cell view, with it's own nstablecellview subclass. Include the array controller in the nib and create an outlet to it in your cell view subclass.
Register it with the table view in the viewDidLoad method of the tables view controller:
NSNib *cellView = [[NSNib alloc] initWithNibNamed:#"MyTableCellView" bundle:nil];
[myTableView registerNib:cellView forIdentifier:#"myTableCellView"];
Then, in the awakeFromNib method of your cell view subclass, manually make your bindings that require the object value:
[self.arrayController bind:#"contentSet"
toObject:self
withKeyPath:#"objectValue.tracks"
options:nil];
Voila.
Note that when using this technique, the files Owner of the nib file is not your nstablecellview subclass, it is the view controller of the table view.
The problem lies in not understanding the MVC-pattern (Model-View-Controller). Content for a view never comes from another view, it comes from model objects via a controller. The content of each of your tableviews always comes from an NSObjectController, or a subclass like NSArrayController. Basically, there are two solution:
bind the 'tracks' tableview to the selection of the 'album' array controller
create a 'tracks' array controller and bind it to the selection of the 'album' array controller. Bind the 'tracks' tableview to the 'tracks' array controller
A third solution is to use an NSTreeController with an NSOutlineView but outline views and tree controllers are notoriously hard to work with.

NSTableView rebuilds all cells when adding only one

I have a view-based NSTableView driven by an NSArrayController bound to a NSMutableArray as the content array.
When I add a new item to the data source, tableView:viewForTableColumn:row: is called for every item in the data source. I would expect the table view to leave existing items alone, and only call this for the new item.
I add a new item via the controller, like this:
[_arrayController addObject:newObject];
How do I prevent the table view from doing this? The performance impact is unacceptable.
Edit: It also clears the user selection

NSTableView - Selecting index by typing

BACKGROUND:
I have a tableview with a list of names. When you click on a name in the list, it displays additional detail information in another section of the window. Everything is connected and is working correctly.
However...
I would like to use Type Select with this table and have run into the following snag:
When I start typing a name (while the table is selected) it correctly highlights the appropriate name in the table BUT the detailed information to the right of the table does not change.
I know the reason is the code for changing the detail information is in an IBAction method which is only called when you click to select a name in the list, and uses the [sender clickedRow] call to get the index of the selected name.
I also suspect that I need to use the [tableView selectedRow] (since it is being selected, but you are not clicking on it) but I am not quite certain where or how to perform this check.
I'm also thinking that since "type select" isn't sending an action message, I won't be able to use [sender selectedRow] but rather will use [tableView selectedRow]...
QUESTION:
How can I tell when the selected row in a tableview has changed via type select?
Thanks!
Implement the tableViewSelectionDidChange: delegate method in your controller, and ensure that your controller is the NSTableView's delegate.
You could update your view with something like:
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification {
[relatedView updateInformationFromRow:[[aNotification object] selectedRow]];
}
From the documentation:
tableViewSelectionDidChange: Informs the delegate that the table
view’s selection has changed.
Within that, you would update your related view. You could use the aNotification's object, which will be a NSTableViewSelectionDidChangeNotification, again from the documentation, is:
Posted after an NSTableView object's selection changes. The
notification object is the table view whose selection changed. This
notification does not contain a userInfo dictionary.

Is it possible to have a button in each row of a table view in Xcode?

I have a table view loaded with 3 pieces of data in each row, formatted using a Table View Cell in IB. I would like to "hide" one piece of data with a button, that when pushed, will then be hidden so that the data that is behind the button will then be displayed.
Does anyone know if this is possible? I put my button in the Table View Cell, but I can't figure out how to get it set to the proper class in the File Owner.

Resources