Cocoa's newbie question: is it possible to bind a NSTableView's selection to another tableview's selection?
Posted
by
cocoaOverloaded
on Stack Overflow
See other posts from Stack Overflow
or by cocoaOverloaded
Published on 2011-02-02T13:23:48Z
Indexed on
2011/02/02
15:26 UTC
Read the original article
Hit count: 205
cocoa
|cocoa-bindings
http://img651.imageshack.us/img651/6999/modelsf.jpg
Let'say, I've 2 entities in the Core Data's Model file, one being all "transactions" ever done by X company. The "transactions" entity has among other properties, a "DATE" property and a to-one relationship "COMPANY"(specifying the company with which X company has done that particular transaction). The other entity:"companies" of course contains all the companies' info ,with which X company has done transaction. The "companies" entity has a to-many relationships "TRANSACTIONS" which is an inverse relationship to "transactions" entity's "COMPANY" relationship.
So within IB, I created a NSTableView(with its own NSArrayController) showing all the transactions on a particular Date (with the help of NSPredicate).
Then I create another table view showing the to-many relationship "TRANSACTIONS" of the company of the selected transaction in the first table view(which shows transactions on a particular date). The 2nd table view's NSArrayController binding is like this:
** bind to: "name of the first tableview's controller", Controller Key: selection, Model Key Path:COMPANY.TRANSACTIONS(the to-many relationship in the "companies" entity)**
Everythings work fine up to this moment, the 2nd tableview shows all the transactions X company has done with the company of the selected transactions in the 1st table view.
But I have a group of textfields showing details of a particular transactions. Binding the these textfields with the controller of the 1st table view(the one showing transactions on a particular date) is pretty straightforward. But what I want to do are: 1/ Look up the transactions on a particular date in the first table view, select any one of them 2/ Then, check all previous transactions of the company of that transaction( selected in the first table view) from the 2nd table view 3/ Select any previous transactions and check the details of the transaction from that group of textfields
So naturally I should have bind the textfields' gp to the 2nd table view's controller. But I found the default selected row in the 2nd table view(the one show all previous transactions of a company) wasn't the transaction I've selected in the 1st tableView for a particular date. Of course, i can manually select that transaction in the 2nd table view again....
So I just want to know if it's possible to have the 2nd table view automatically select the transaction according to the transaction I've selected in the 1st table view thr binding??
After hours of googling, I solved the problem by implementing the tableview Delegate protocol:
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification {
if (["nameOf1stTableView" selectedRow] > -1) {
NSArray *objsArray = ["nameOf2ndTableView'sController" arrangedObjects];
for (id obj in objsArray) {
if ([[obj valueForKey:@"DATE"] isEqualToDate: ["nameOf1stTableView'sController".selection valueForKey:@"DATE"]]) {
["nameOf2ndTableView" selectRowIndexes:[NSIndexSet indexSetWithIndex:[objsArray indexOfObject:obj]] byExtendingSelection:NO];
}
}
}
}
But,this just look too cumbersome... can it be done with binding alone?
Thanks in Advance,
© Stack Overflow or respective owner