How can I set a UITableView to grouped style
Asked Answered
C

18

98

I have a UITableViewController subclass with sections. The sections are showing with the default style (no rounded corners). How can I set the TableView style to grouped in the code? I'm not using Interface Builder for this, so I need something like

[self.tableView setGroupedStyle]

I searched on Stack Overflow, but couldn't come up with an answer.

Cookshop answered 17/6, 2009 at 12:24 Comment(1)
Swift version: LinkAlleviation
D
111

If i understand what you mean, you have to initialize your controller with that style. Something like:

myTVContoller = [[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];
Doublecheck answered 17/6, 2009 at 12:47 Comment(3)
As stated it is not a property that can be changed. One grouped always grouped.Bifurcate
This is not a correct answer, it's not solving the problem of having a UITableViewController subclass. If you inherit UITableViewController, you have to override the constructor not create another UITableView or UITableViewController.Magistrate
How would you do this if it's a subclassed controller, as stated in the question?Kare
C
144

You can do the following:

UITableView *myTable = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];

Swift 3:

let tableView = UITableView.init(frame: CGRect.zero, style: .grouped)
Condyloid answered 18/8, 2009 at 21:49 Comment(4)
THIS IS THE REAL SOLUTION! THANKS. The accepted answer is wrong, at least for OS 3.0+Frankly
Please note that there was a typo in the accepted solution. I've fixed it now. Both answers work for me, but they instantiate different things. This answer is a UITableView, the accepted answer is a UITableViewController.Cookshop
Actually, the accepted answer is misleading. Please read what the OP said "I have a UITableViewConotroller subclass". The accepted answer ONLY gives you a plain jane UITableViewController; without knowing details it is impossible to solve the users problem regarding the subclass. This answer and using self.myTable would probably be better, IMHO.Anyway
This also works with constraints on iOS8 (don't know about iOS7)...just initialize with a frame of CGRectZero along with your tableview style and then add your constraints and you get the right style along with your constraints.Consciousness
D
111

If i understand what you mean, you have to initialize your controller with that style. Something like:

myTVContoller = [[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];
Doublecheck answered 17/6, 2009 at 12:47 Comment(3)
As stated it is not a property that can be changed. One grouped always grouped.Bifurcate
This is not a correct answer, it's not solving the problem of having a UITableViewController subclass. If you inherit UITableViewController, you have to override the constructor not create another UITableView or UITableViewController.Magistrate
How would you do this if it's a subclassed controller, as stated in the question?Kare
S
21

I give you my solution, I am working in "XIB mode", here the code of a subclass of a UITableViewController :

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    return self;
}
Sensitivity answered 28/5, 2011 at 17:30 Comment(1)
Caveat to this solution - if this is under a navigationController in the NIB, it will lose that reference and make you really confused.Koonce
D
15

Below code Worked for me, I am also using UITableview class

- (id)initWithStyle:(UITableViewStyle)style
{
     self = [super initWithStyle:UITableViewStyleGrouped];

     if (self)
    {

     }
    return self;
}
Dalston answered 3/3, 2014 at 11:23 Comment(1)
This is the right solution for people using a subclass of UITableViewController. Thanks!Dysteleology
E
13

If you are inheriting UITableViewController, you can just init tableView again.

Objective C:

self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];

Swift:

self.tableView = UITableView(frame: CGRect.zero, style: .grouped)
Extremity answered 6/9, 2017 at 7:1 Comment(0)
P
10

Setting that is not that hard as mentioned in the question. Actually it's pretty simple. Try this on storyboard.

enter image description here

Pretense answered 15/7, 2017 at 2:29 Comment(1)
If you're already having a tableView as part of a TableViewController setting it this way is the best. Other answers focus more on instantiating a new tableView, etc.Enervate
A
5

Swift 4+:

let myTableViewController = UITableViewController(style: .grouped)
Alleviation answered 26/2, 2016 at 21:25 Comment(0)
A
3

Swift 4

Using Normal TableView

let tableView = TableView(frame: .zero, style: .grouped)

Using TPKeyboardAvoidingTableView

let tableView = TPKeyboardAvoidingTableView(frame: .zero, style: .grouped)
Aperient answered 8/11, 2017 at 7:27 Comment(0)
C
3

If you create your UITableView in code, you can do the following:

class SettingsVC: UITableViewController {

    init() {
        if #available(iOS 13.0, *) {
            super.init(style: .insetGrouped)
        } else {
            super.init(nibName: nil, bundle: nil)
        }
     }
    
     @available(*, unavailable)
     required init?(coder aDecoder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }
 }
Caterinacatering answered 26/2, 2021 at 10:56 Comment(1)
IMHO, this is currently the right answer. Create an init method, where all the properties needed to work are initialized, and then call super.init(style: .insetGrouped).Vaden
E
2

For set grouped style in ui itself:-Select the TableView then change the "style"(in attribute inspector)) from plain to Grouped.

Eolith answered 26/3, 2014 at 5:39 Comment(0)
A
2

You can also do this if you want to use it on a subclass you've already created in a separate swift file (probably not 100% correct but works)

override init(style: UITableViewStyle) {
    super.init(style: style)
    UITableViewStyle.Grouped
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

Now in you appdelegate.swift you can call:

let settingsController = SettingsViewController(style: .Grouped)
Apotheosis answered 28/7, 2016 at 20:24 Comment(0)
D
1

You can do this with using storyboard/XIB also

  1. Go To storyboard -> Select your viewController -> Select your table
  2. Select the "Style" property in interface-builder
  3. Select the "Grouped"
  4. Done
Daugava answered 26/9, 2016 at 11:57 Comment(0)
L
0

If you have one TableView for more tables, and one of this tables is grouped and the another one plain, than you can simulate the plain style with the function from UITableViewDelegate:

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {   
    return CGFloat.min
}
Liar answered 23/9, 2016 at 16:13 Comment(0)
R
0

swift 4

if you don't want use storyboard, this might be help.

you can add table view and set properties in a closure:

lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .grouped)

        tableView.backgroundColor = UIColor(named: Palette.secondaryLight.rawValue)
        tableView.rowHeight = 68
        tableView.separatorStyle = .none
        tableView.translatesAutoresizingMaskIntoConstraints = false
        return tableView
    }()

then add in subview and set constraints.

Rickie answered 14/3, 2019 at 6:52 Comment(0)
A
0

If you're not using Storyboards and just want a clean, concise way to setup your UITableViewController subclass to use a different tableView style, you can simply override loadView like this:

override func loadView() {
    let tableView = UITableView(frame: .zero, style: .grouped)
    tableView.delegate = self
    tableView.dataSource = self
    view = tableView
}

As the documentation says:

loadView creates the view that the controller manages it's a method you can ovveride when using ViewControllers whose views are not defined via Storyboards or NIBs.

The UITableViewController implementation already overrides this method for you to crate a simple, plain UITableView as the root view for the controller, but nothing stops you from further overriding it to create a table view that better suits your needs.

Just remember:

Your custom implementation of this method should not call super.

Although answered 28/10, 2022 at 14:58 Comment(0)
H
-5

You can also try to make the separator line color clear which could give the grouped style effect:

[myTVContoller.tableView setSeparatorColor:[UIColor clearColor]];
Halfcaste answered 6/7, 2010 at 18:12 Comment(1)
Sections will not collapse this way.Cinch
D
-11

You can use:

(instancetype)init {
return [[YourSubclassOfTableView alloc] initWithStyle:UITableViewStyleGrouped];
}
Delastre answered 27/3, 2012 at 6:36 Comment(0)
C
-28
self.tableView.style = UITableViewStyleGrouped

EDIT:

Had assumed this was a read/write property. In that case, you can either follow Dimitris advice and set the style when you instantiate the controller, or (if you're using a XIB), you can set it via IB.

Confederacy answered 17/6, 2009 at 12:45 Comment(2)
Thanks for the reply. I'm not using an XIB, I've a tab controller with a subview which is my custom class. Not sure where I can put the initWithStyle call, I'm not doind the TableView init, it's done by the tab controller. Changing the initWithStyle in the TableViewController doesn't seem to work.Cookshop
As dimitris said, if you're not using a XIB, you must be init'ing the UITableViewController somewhere. That's where you want to use the initWithStyle: call.Confederacy

© 2022 - 2024 — McMap. All rights reserved.