Chapters

Hide chapters

iOS Apprentice

Eighth Edition · iOS 13 · Swift 5.2 · Xcode 11

Getting Started with SwiftUI

Section 1: 8 chapters
Show chapters Hide chapters

My Locations

Section 4: 11 chapters
Show chapters Hide chapters

Store Search

Section 5: 13 chapters
Show chapters Hide chapters

33. The Locations Tab
Written by Eli Ganim

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

You’ve set up the data model and given the app the ability to save new locations to the data store. Next, you’ll show these saved locations in a table view in the second tab.

The completed Locations screen will look like this:

The Locations screen
The Locations screen

This chapter covers the following:

  • The locations tab: Set up the second tab to display a list of saved locations.
  • Create a custom table view cell subclass: Create a custom table view cell subclass to handle displaying location information.
  • Edit locations: Add functionality to allow editing of items in the locations list.
  • Use NSFetchedResultsController: How do you use NSFetchedResultsController to fetch data from your Core Data store?
  • Delete Locations: Add the ability to the UI to delete locations, thus removing them from the Core Data store as well.
  • Table view sections: Use built-in Core Data functionality to add the ability to display separate sections based on the location category.

The Locations tab

➤ Open the storyboard editor and delete the Second Scene. This is a leftover from the project template and you don’t need it.

➤ Drag a new Navigation Controller on to the canvas — it has a table view controller attached to it, which is fine. You’ll use that in a second.

➤ Control-drag from the Tab Bar Controller to this new Navigation Controller and select Relationship Segue — view controllers. This adds the navigation controller to the tab bar.

➤ The Navigation Controller now has a Tab Bar Item that is named “Item.” Rename it to Locations.

➤ Double-click the navigation bar of the new table view controller (the one attached to the new Navigation Controller) and change the title to Locations. (If Xcode gives you trouble, use the Attributes inspector on the Navigation Item instead.)

The storyboard now looks like this:

The storyboard after adding the Locations screen
The storyboard after adding the Locations screen

➤ Run the app and activate the Locations tab. It doesn’t show anything useful yet:

The Locations screen in the second tab
The Locations screen in the second tab

Designing the table view cell

Before you can show any data in the table, you first have to design the prototype cell.

Jyo lwubikrpu valb
Vza xzamiplsu zivl

The basic table view controller

Let’s write the code for the view controller. You’ve seen table view controllers several times now, so this should be easy.

Rzu vulxa keow tobx buza jivi
Tge verna piat qawh vegi hagi

Displaying Locations from data store

➤ Run the app and tag a handful of locations. If there is no data in the data store, then the app doesn’t have much to show…

Svo gotp of Bufakeefl
Vli tisz ov Momuhiohv

Creating a custom table view cell subclass

Using viewWithTag(_:) to find the labels from the table view cell works, but it doesn’t look very object-oriented to me.

Editing locations

You will now connect the LocationsViewController to the Location Details screen, so that when you tap a row in the table, it lets you edit that location’s description and category.

Creating edit segue

➤ Go to the storyboard. Select the prototype cell from the Locations scene and Control-drag to the Tag Locations scene (which is the Location Details screen). Add a Show selection segue and name it EditLocation.

Ywo Xediqiom Hamaoxy vvpeos ag ken axko cetzilhih ko jlo Fegudoafy myfion
Vxe Majucaid Hewaapt qwhouh eq div ihte jildushud be hwa Xuvedoinx pbguaq

The Any type

The type of the sender parameter is Any. You have seen this type in a few places before. What is it?

Setting up the edit view controller

When editing an existing Location object, you have to do a few things differently in the LocationDetailsViewController. The title of the screen shouldn’t be “Tag Location” but “Edit Location.” You also must put the values from the existing Location object into the various cells.

Ogiyimk am ikekfagr diboluax
Inizexm at irodxulq tecalooz

Fixing the edit screen

There are two problems to solve:

Using NSFetchedResultsController

As you are no doubt aware by now, table views are everywhere in iOS apps. A lot of the time when you’re working with Core Data, you want to fetch objects from the data store and show them in a table view. And when those objects change, you want to do a live update of the table view in response, to show the changes to the user.

Organizing the code using extensions

An extension lets you add code to an existing class, without having to modify the original class source code. When you make an extension you say, “here are a bunch of extra methods that also need to go into that class,” and you can do that even if you didn’t write the original class to begin with.

“It’s not a bug, it’s an undocumented feature”

There is a nasty Core Data bug that has been there for the last few iOS versions. Here is how you can reproduce it:

Deleting locations

Everyone makes mistakes. So, it’s likely that users will want to delete locations from their list at some point. This is a very easy feature to add: you just have to remove the Location object from the data store and the NSFetchedResultsController will make sure it gets dropped from the table — again, through its delegate methods.

Dbuko fi korodo fufc gfev jna jashi
Lcase ha tirane nagy twux wla loxke

Swe himhi viut aw ajer nule
Hha felbo vueb em uyax kifi

Table view sections

The Location objects have a category field. It would be nice to group the locations by category in the table. The table view supports organizing rows into sections and each of these sections can have its own header. Putting your rows into sections is a lot of work if you’re doing it by hand, but NSFetchedResultsController practically gives you section support for free.

Vbu koroyoofx uca tuc hvoibud ic dodyuect
Pke calujiomc ope sog vceizuc us jaqxiudk

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now