Smallest table
Xcode version: 8.2.1, Swift version: 3, Date: 2017-01-23
This example shows how to bind a table to a simple array, but without core data or binding controllers. Everything is done in code, with just a little dusting of value-binding in the tableView itself.
Aside from the table stuff, it also contains code that opens a user dialogue to solicit a file selection from the user (NSOpenPanel).
The table will look as follows:
The table has two columns. In it it shows the filename and the size of that file. It also has two buttons, one to add entries to the table, and one to remove entries from the table.
The main ingredients for this example are an array and a table view. The array is defined in the AppDelegate, an since the tableview contains two fields, the table is defines as a table of struct’s where each entry has two values:
We need access to the table view and the remove button, for simplicity the outlets for these are also defined in the AppDelegate:
The setup of the xib is straightforward. We use the AppDelegate as the data source and as the table view delegate, so do not forget to wire these outlets as well. Otherwise there is nothing fancy until we get to the way in which the tableview retrieves the values it needs for the table cells. For this we use a value binding. Select the table view cell as shown in the image below.
Notice in the above image that the table rows are titled “File” and “Size”. This is important as these values will be used to identify the table column in the source code later.
Then select the value binding as shown below:
And bind the value to the Table Cell View for the key ‘objectValue’. This is all done by a simple click in the “Bind to” checkbox.
Once that is done, the table view will use the tableView:objectValueFor:row function from the TableViewDataSource protocol to retrieve the value of the cell. That function can be implemented as follows:
The only other TableViewDataSource protocol function we need is the one that tells the table how many rows there are:
With the above, the table works. However there is no data in the table yet. For this we use the “add” button. It works by opening a dialogue that asks the user to select a file (or files):
Almost done now. The only remaining action is to implement the remove button. Both the removal itself and the enabling/disabling of the button based on the status of the table selections. That last bit is easy:
To have this called every time the user selects or deselects a row, implement this function from the TableViewDelegate protocol:
The final bit of code removes rows that were selected when the user clicked the “remove” button: