UITableView
iOS Development
Grouped Style
Swift
UITableViewStyleGrouped

How can I set a UITableView to grouped style

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

UITableView style is chosen when the table view is created, not later. If you want grouped sections, the correct approach is to initialize the table with .grouped or configure that style in Interface Builder before the view is instantiated.

Create the Table View with .grouped

When building a table view in code, pass the style in the initializer:

swift
1import UIKit
2
3final class SettingsViewController: UIViewController, UITableViewDataSource {
4    private let tableView = UITableView(frame: .zero, style: .grouped)
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8
9        view.addSubview(tableView)
10        tableView.frame = view.bounds
11        tableView.dataSource = self
12        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
13    }
14
15    func numberOfSections(in tableView: UITableView) -> Int {
16        2
17    }
18
19    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
20        2
21    }
22
23    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
24        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
25        cell.textLabel?.text = "Section \(indexPath.section), Row \(indexPath.row)"
26        return cell
27    }
28}

That is the standard programmatic solution. The grouped appearance comes from the initializer argument, not from a later property assignment.

Use UITableViewController(style:) When Appropriate

If your screen is a UITableViewController, Apple gives you a style-aware initializer:

swift
1import UIKit
2
3final class ProfileViewController: UITableViewController {
4    init() {
5        super.init(style: .grouped)
6    }
7
8    required init?(coder: NSCoder) {
9        super.init(coder: coder)
10    }
11}

This is cleaner than creating a plain view controller and manually managing the table if the whole screen is fundamentally a table view.

Storyboards and Nibs

If the table view comes from a storyboard or nib, choose the style in Interface Builder before runtime. The important limitation is that the style is baked into the created table view instance. Once the table view exists, there is no supported API to flip it from plain to grouped in place.

That means if a storyboard scene was created as a plain table view and you later decide it should be grouped, you need to change the storyboard setting or recreate the table view programmatically with the desired style.

Grouped Versus Inset Grouped

Modern iOS also offers .insetGrouped, which is visually different from classic grouped tables. It is often used in settings-style screens:

swift
let tableView = UITableView(frame: .zero, style: .insetGrouped)

Use .grouped if you want the traditional grouped look and .insetGrouped if you want the newer inset style. The correct choice is mostly visual and depends on the design of the screen.

Why You Cannot Change the Style Later

Developers often look for something like:

swift
tableView.style = .grouped

That API does not exist. The style affects the internal configuration of the table view from the start, so Apple exposes it as an initialization choice, not as a mutable setting.

If you truly need to switch styles dynamically, the practical approach is to replace the old table view with a newly created one using the new style and then reconnect its data source, delegate, and constraints.

Common Pitfalls

The most common mistake is trying to change the style after the table view has already been created. That is not how UITableView is designed.

Another pitfall is using a storyboard-created table view and then searching for a runtime property to switch it to grouped. In storyboard-driven code, the style must be changed in the storyboard itself or by replacing the table view.

It is also easy to confuse .grouped and .insetGrouped. They are related but visually distinct, and the wrong one can make the screen feel out of place next to the rest of the app.

Finally, if your controller is entirely table-driven, UITableViewController(style:) is often simpler than embedding a table view manually in a plain UIViewController.

Summary

  • Set grouped style at creation time with UITableView(frame:style:).
  • Use UITableViewController(style:) when the whole screen is a table.
  • For storyboard scenes, choose the style in Interface Builder before runtime.
  • You cannot mutate a table view from plain to grouped after it has been created.
  • Consider .insetGrouped if you want the newer grouped appearance on modern iOS.

Course illustration
Course illustration

All Rights Reserved.