Table view not updating according to bindings - Pa

2019-07-21 01:35发布

问题:

Title borrowed from this question, of which this one is not a duplicate. See my answer there for what was wrong for that questioner; I'm the author of that answer, and my problem is not that one.

I have a table view with three columns, whose Value bindings are bound to three properties of the arrangedObjects of an array controller. The array controller's contentArray is bound to the visitationResults of my document object; the items in that array are instances of a model class (VisitationResult). I have also bound the array controller's selectionIndexes and sortDescriptors to properties of my document.

I am mutating my property through a couple of accessors:

- (void) addVisitationResult:(VisitationResult *)newVisitationResult {
    [self insertObject:newVisitationResult inVisitationResultsAtIndex:[self countOfVisitationResults]];
    NSLog(@"arrayController arrangedObjects: %@", [arrayController arrangedObjects]);
}

That NSLog statement runs, and confirms that the array controller is gathering and arranging my model objects. This means that I am going through my property and getting KVO notifications for my document (which proves that the earlier questioner's problem, that of bypassing the property, is not the problem I'm having).

I added NSLog statements in my model object class's accessor methods. One of them is being called—by the array controller, in order to sort the objects (that property is the sort key). The other two, which the array controller doesn't know about, never get called.

Thus, my table view remains blank.

回答1:

I found the problem: It's because I had explicitly bound the selectionIndexes and sortDescriptors bindings of the table view.

This wasn't necessary, anyway: I just checked, and the documentation says:

selectionIndexes

Typically, selectionIndexes is bound automatically to the NSArrayController that the first NSTableColumn is bound to.

sortDescriptors

Typically this binding is created automatically, binding to the sort descriptors of the NSArrayController of the initially bound NSTableColumn.

It appears that not only is it not necessary, but binding either or both of these two will break the table view.