Build Swift Table View Static Cells Programmatically
Present Table View with Static Cells (Rows) Programmatically.
Scenario
Settings table view, constructed by Swift code with a minimal UI Storyboard, and presented with section headers and detail rows, linked to segues for navigation to individual views.
Audience
The article is for Swift developers who seek complete, integrated, code-centric solutions to speed up their development projects.
Model
We based the article on the AppsGym Books model app, which is published on Apple’s App Store (as 8Books). You can review the Settings page in the app and/or download the complete Xcode project on AppsGym.com, totally free.
UI
The Settings view menu shows a list of options/functions for user selection. The Settings table view utilises programmed section headers (Setup, Support, etc.) to group related records (e.g., Support: Info, Credits, Privacy, etc.) in a mini section within the overall list.
Books Storyboard Settings Scene sets up table view with a minimal Storyboard elements (image, 1 prototype basic cell, and segues to detailed views), while SettingsTableViewController.swift controls the layout and navigation.
Logic
- Declare variables for the arrays of sectionTitles (e.g., Support) and sectionContent (e.g. Info)
- Set up TableView sections, rows and cells, using sectionTitles and sectionContent
- Configure the cell in cellForRowAt(..), with each cell corresponding to a table row
- didSelectRowAt(..) will performSegues to navigate to the detailed views
Code
The Swift code snippets below are extracts from SettingsTableViewController.swift , which displays and controls the Settings page.
SettingsTableViewController.swift viewDidLoad()
SettingsTableViewController.swift Section Functions
SettingsTableViewController.swift cellForRowAt()
SettingsTableViewController.swift didSelectRowAt()
Navigation Segues are setup on the Storyboard but you can use prepare(for segue) if you need to pass any variable values to called ViewController.
SettingsTableViewController.swift prepare(for segue): Optional. Replace the ‘_’ by a ‘let vcName ‘ if needed to pass data to called ViewController, example: case “showAppInfo” below.
The article showed the complete logic and code to present a Settings menu view, purely by Swift code, with minimal Storyboard setup (1 cell and the segues to detailed views). Thanks for reading!