P. 169: Selections, selection indexes, selection objects

This whole section needs a rewrite. I’m afraid that some readers, after enduring the physics example in Chapter 3, will quit the book when they come to this section.

The discussion about selections should have been introduced with something like this:

Because that wasn’t clearly stated up front, the whole section was incomprehensible to me. The only thing I understood was the last paragraph on p. 170, which essentially says the same thing as my proposed introduction. With that understanding, I reread the section at least another 10 times, and I think I finally figured out what the text was trying to say. Here are the key points:

  1. As was stated previously in the chapter, the Array Controller’s Content Array(its content property) and the arrangedObjects array are in a different order. I’m not sure why that needed repeating here.

  2. The Array Controller considers the selected Employee to be the last Employee that was added to the table.

  3. The TableView considers the highlighted row to be the selected Employee.

  4. Therefore, if the highlighted row is not the last Employee that was added to the table, then the Array Controller and the TableView will have different notions of which Employee is selected.

  5. The Remove button removes the Employee that the Array Controller considers to be the selected Employee. The text says that the Employee is removed from the Content Array, but I would think that the Employee would also have to be removed from the arrangedObjects array. Indirectly, I think the Employee is removed from the arrangedObjects array, but remember that the arrangedObjects array is the result of filtering and sorting the Content Array. So directly deleting something from the arrangedObjects array wouldn’t change the Content Array, and therefore the next time the Content Array was filtered and sorted anew to create the arrangedObjects array, the removed Employee would reappear in the arrangedObjects array.

  6. To get the Array Controller and the TableView in sync–so that they both consider the same Employee to be the selected Employee–you can bind the TableView’s Selection Indexes binding to the Array Controller’s selectionIndexes property.

Now to me, it makes more sense to do it the other way around and bind the Array Controller to the TableView because we want the TableView to determine the selection, and we want the Array Controller to read the selection from the TableView. So I tried to do the binding the other way around, but I couldn’t find a way to do it.