How can I set a UITableView to grouped style
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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:
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:
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:
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:
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
.insetGroupedif you want the newer grouped appearance on modern iOS.
Related reading
- How can I set aspect ratio constraints programmatically in iOS?
- How can I set the focus and display the keyboard on my EditText programmatically
- How can I set the title of a UIButton as left-aligned?
- How can I set tint for an image view programmatically in Android?
- How can I take an UIImage and give it a black border?
- How can I test Apple Push Notification Service without an iPhone?
- How can I use a Swift enum as a Dictionary key? Conforming to Equatable
- How can I use Autolayout to set constraints on my UIScrollview?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.