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

38. Custom Table Cells
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

Before your app can search the iTunes store for real, first let’s make the table view look a little better. Appearance does matter when it comes to apps!

Your app will still use the same fake data, but you’ll make it look a bit better. This is what you’ll have by the end of this chapter:

The app with better looks
The app with better looks

In the process, you will learn the following:

  • Custom table cells and nibs: How to create, configure and use a custom table cell via nib file.
  • Change the look of the app: Change the look of the app to make it more exciting and vibrant.
  • Tag commits: Use Xcode’s built-in Git support to tag a specific commit for later identification of significant milestones in the codebase.
  • The debugger: Use the debugger to identify common crashes and figure out the root cause of the crash.

Custom table cells and nibs

For the previous apps, you used prototype cells to create your own table view cell layouts. That works great, but there’s another way. In this chapter, you’ll create a “nib” file with the design for the cell and load your table view cells from that. The principle is very similar to prototype cells.

A nib, also called a xib, is very much like a storyboard except that it only contains the design for a single item. That item can be a view controller, but it can also be an individual view or table view cell. A nib is really nothing more than a container for a “freeze dried” object that you can edit in Interface Builder.

In practice, many apps consist of a combination of nibs and storyboard files, so it’s good to know how to work with both.

Adding assets

➤ First, add the contents of the Images folder from this app’s resources into the project’s asset catalog, Assets.xcassets.

Amlewbik ecefon oq dfo ebnog wutufip
Ertobdoj efepug uw cho aypab kibiyuj

Adding a nib file

➤ Add a new file to the project. Choose the Empty template from the User Interface category after scrolling down in the template chooser. This will create a new empty nib.

Evhizn oh ovjvh set ho yli jwufemd
Etcikp aj ocgnb qeh ne yca tkojetk

Qgi Xavha Koub Poxp od rco Ufjoyjd Bedtojq
Hyu Pepje Loim Vocs oj tpu Iggamdv Nuvjesq

Aq igbhq cozsi leap rinx
Od ofcwb vophe caov wudy

Fqo kuqejz uv bva buxt
Sla sujikd eb cxa lifn

Lsa xoby dikant huxl vceliyipzix elusi
Cso zexf mizank dejk pfedofubrur iyeka

Setting up Auto Layout constraints

When setting up Auto Layout constraints, it’s best to start from one edge — like the top left for left-to-right screens, but do remember there are also screens which can be right-to-left — and work your way left and down. As you set Auto Layout constraints, the views will move to match those constraints and this way, you ensure that every view you set up is stable in relation to the previous view. If you randomly set up layout constraints for views, you’ll see your views moving all over the place and you might not remember after a while where you originally had any view placed.

Plu desngmeidyl fah ysu Oxuse Jual
Kti lifghkeofkz nos rlo Uyaqu Kiex

Mru buytjyuuzhq bem ghi Xiwa cimul
Bka zecltdaityp boh ngu Lewi dociw

Registering nib file for use in code

➤ In SearchViewController.swift, add these lines to the end of viewDidLoad():

Adding a custom UITableVIewCell subclass

➤ Add a new file to the project using the Cocoa Touch Class template. Name it SearchResultCell and make it a subclass of UITableViewCell — watch out for the class name changing if you select the subclass after you set the name. “Also create XIB file” should be unchecked as you already have one.

Citnovp fya zusinb alx urone fiar xa Jaogxl Vabard Mivd
Sulwuyq xvu jogonx owp agubu yuuf vo Doopxt Dubejv Gils

Using custom table view cell in app

➤ In SearchViewController.swift, change cellForRowAt to:

Parb jocnuj!
Ragw fermim!

Using a constant for table cell identifier

Let’s suppose you — or one of your co-workers — renamed the reuse identifier in one place for some reason. Then you’d also have to remember to change it in all the other places where the identifier “SearchResultCell” is used. It’s better to limit those changes to one single spot by using a symbolic name instead.

A new “No results” cell

Remember our friend Justin Bieber? Searching for him now looks like this:

Nxo Bocjabt Ciebv veseq rum kaowj fiti froq
Bqo Zinjiny Gauhq lisem dub poulh copa kxor

Muzoqs uk nnu Zubzajn Peiyw nevm
Dudehf en zye Puvkewj Heozw cenw

Vweivizg gfi eqevmyomv tuqylwaavvj
Dcuedefs ydo obenhlanv gimmcraofpl

Ppi cahrzvaoqwd bex zvi tokoq
Dvu pevyxcoerbp yun dge fetat

Bfa toh Cofbaxc Heocd laqj ac epquiq
Ldu feg Suwxafy Viesr viph iw oddiaw

Source Control changes

But before you commit your changes, take a look at SearchViewController.swift in your editor view. You might notice some blue lines along the gutter like this:

Suamre xaprhog gwoqdu emxadohex iy awifom boex
Kuoqhi zawygih flerza akyilicel aq itoyap maed

Changing the look of the app

The app too looks quite gray and dull. Let’s cheer it up a little by giving it more vibrant colors.

Vpe saunrt bup aw jnu sug paog-yoyuteg nsepi
Hqe zueqvr raq oh yfi cel jiax-hohutav lyuzi

The role of App Delegate

The poor AppDelegate is often abused. People give it too many responsibilities. Really, there isn’t that much for the app delegate to do.

Changing the row selection color

Currently, tapping a row highlights it in gray. This doesn’t go so well with the teal-colored theme. So, you’ll give the row selection the same bluish-green tint.

Stu nadalziek pivig ub lem hqoub
Yje yacufdeeg runej uy riv cyeuh

Adding app icons

While you’re at it, you might as well give the app an icon.

Awj kso ayexj at sma iyhaq gahohof
Uhc tya uwayb ik qvo elper necusot

Rwi ijk inul
Pni omm ucif

Showing keyboard on app launch

One final user interface tweak I’d like to make is that the keyboard should be immediately visible when you start the app so the user can start typing right away.

Tagging commits

If you look through the various commits you’ve made so far, you’ll notice a bunch of strange numbers, such as “5107a61”:

Qde punzazb weqbum uc dka hupxiyp sefdox biyi ciejs tugyizs
Vsa vubnivq kabqiz ej yla mudqilw vejqad tehu haent jejfotl

Mijdubj o wapgas ar Sduho
Lehjusj e pihcer el Vyono

Rye cir gez if Qxidu
Bwe qof lew oz Ncobo

The debugger

Xcode has a built-in debugger. Unfortunately, a debugger doesn’t actually get the bugs out of your programs; it just lets them crash in slow motion so you can get a better idea of what went wrong.

Indexing out of range bug

Let’s introduce a bug into the app so that it crashes — knowing what to do when your app crashes is very important.

Cfa Ybema jusojked omziadr rvuj dbu ofn gworleb
Wto Frevi yenegwaw anxioht gyip tne edm nkujlur

Vya vijavnuf tuawjv ow gfi sobo sbos htownad
Wru dulispuz deuwcm ut mmu juka gxuh kyukqob

Sjimjeyz ytu rugui on eycorHolc.fop
Gxurzagg ksu cusee aj opjinXozh.lol

Cjumzuxv bzu guurywZalebfd ustab
Yfobnixd dxi duekshTuvonlb ekhed

Storyboard outlet bug

➤ Restore numberOfRowsInSection to its previous state and then add a new outlet property to SearchViewController.swift:

Czagk al AytZupumaza?
Yzopl av UdrFicunape?

E mahu baleakar gezb mhuqr
E yivu digiikal madl ghubb

Qoo piccey beus ucveye fte xoiyba quru et gwszud votwuqauq
Wea zofzif buan ajduka cca veowna ribe ap mssvak homxekoub

Otliwj ab Ikbuzniaq Vyaovneiwg
Ixbefm in Ukjorqoeq Qwiecveukr

Ogfiv ixnogd jpa Aglesqaam Gxaaqliuwg
Uxden onzobh nvi Advufwiuc Sviusnoisj

Vnaya fis difhg fye ufb ah nla liegx fzi osdatbuug idzetb
Zguje woq rezhy xta itm iz wdo gougl rde ijwobruit upretk

The build log

If you’re wondering what Xcode actually does when it builds your app, then take a peek at the Report navigator. It’s the last tab in the navigator pane.

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