Create a new iPad playground. Learn how to use the UITableView Controller in playgrounds. Learn more techniques for coding in iPad playgrounds.
- [Instructor] You've now made a list using a text view. That works great for a menu board, but not for ordering pizzas. For that, we need a table view. You may use table view controllers frequently in Playgrounds, since they don't need their own layout. They do all the work themselves. Since we do not have a storyboard to set up a UI table view in Playgrounds, we tend to use the code version. I'll show you how to set up a table view in Playgrounds and, along the way, expand your knowledge of how to enter code into the iPad Playgrounds. Head over to Playgrounds for iPad. Go ahead and close up Xcode.
In the My Playgrounds view, hit the plus and select a blank template. Tap on the word blank, and change the name to Huli Pizza Order, and hit done. Now go into the file. You'll see a completion bar in iPad Playgrounds. By typing some combination of letters, it intelligently guesses what you want.
You can scroll to see more of the suggestions, or keep typing to get a better guess. It doesn't completely care about case, by the way, but it will take case into consideration. On the keyboard, which you can get by pressing the little caret, type I-M, and the autosuggestions will show the import keyword. Tap that, and then type U-I-K, for UIKit. UIKit appears, tap that, and return. Now do the same thing for Playground support.
I-M-P for import, and then P-L-A-Y, and there's Playground support. We'll need a class of view controller. Tap the plus in the upper-right corner. Scroll down to the very bottom and you'll see class. Tap that. Set the class to order table view controller. Put a colon, and then select UI table view controller, which you may have to scroll over a little bit to find.
Tap code in the controller. Tap the plus in the upper-right, and tap there. Set the name to menu items. Tap the value and then pick the array of values, then a shift-S for string. After the array, tap the parentheses to get a set of parentheses. I'll add a property observer on menu items to to update the table any time I've set the list.
This is one of those times autocomplete does a very bad job of knowing what you are doing. Add a curly bracket after the there statement. Press return. On the next line, type in did set. After that, add another curly brace, and then another return. Add in table view dot reload data. When menu items changes in our playground, table view will reload the data in the table, updating the table with the changes.
Under the class, we'll implement it in a live view. So hit let, and then we're going to call this table equals order table view controller and hit the parentheses there, return. And then Playground page dot current dot live view equals table.
I'll leave off the needs infinite execution this time. You can also assign to menu items the list of pizzas. So go ahead and do that. Table dot menu items equals and we have a string, so we can tap on the string, and we're going to use values here. Quit the quotes, and then we're going to start putting in our pizzas. Pepperoni, sausage, cheese Huli chicken, surfboard vegan, deep diver, big island, and sweet home Chicago.
Okay, now you can go run this. You'll get a blank table view but it'll scroll. To get the list into the table, you need to define three methods. In the next part, we'll add those three.
- Comparing iPad and Xcode playgrounds
- Creating and testing Swift classes and code in iPad and Xcode playgrounds
- Prototyping code
- Debugging and testing with Quick Look live views
- Using markup to interactively document code and create educational lessons
- Compiling code into playground books