I think Cocoa Bindings is a little bit controversial technology. Some people believe less code means less error, easier to debug, according to the others it is hard to read and understand, it’s black magic. During the years I tried to understand several times with very little luck. Last night I read a brilliant book by Stuart Grimshaw. I highly recommend his book, he wrote a lot of very useful concept about general programming and Swift coding as well.
This tutorial shows a simple Table View.
Step 1. Create a new Swift project with Storyboard.
Step 2. Add a new file with a Person class, which conforms KVO.
Create an array in
ViewController.swift as data source of the TableView. This will be the last code we need to write.
Move to Main.storyboard and add an
ArrayController from the library.
Make our life easier name this controller as
Click on “Show the Bindings inspector” on top right. Make the changes showed on the picture.
- Bind our new
- Set the
Model Key Pathto
peopleArrayor the name of the array in your
Step 5. Add Table View from the Library.
By default the Table View comes with 2 rows only, add additionl one for mobile number.
Now, connect the Array Controller and the Table View.
Name the columns of Table View.
As a final step, bind the Table Cell View.
Step 6. Build and Run.
Source code: Github
- Stuart Grimshaw - Mastering macOS Programming
- Thomas Grossen - TableView binding with Swift 4 and Storyboard
- Taun Chapman - Bindings, the Best Code is the Code Not Written