Tutorial :How can I set a UITableView to grouped style



Question:

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.


Solution:1

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

myTVContoller = [[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];  


Solution:2

You can do the following:

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

Swift 3:

let tableView = UITableView.init(frame: CGRect.zero, style: .grouped)  


Solution:3

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;  }  


Solution:4

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

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


Solution:5

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

enter image description here


Solution:6

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

Objective C:

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

Swift 4:

self.tableView = UITableView(frame: CGRect.zero, style: .grouped)  


Solution:7

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


Solution:8

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)  


Solution:9

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


Solution:10

Swift 4

Using Normal TableView

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

Using TPKeyboardAvoidingTableView

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


Solution:11

Swift 2.1:

let myTableViewController = UITableViewController(style: UITableViewStyle.Grouped)  


Solution:12

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  }  


Solution:13

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

[myTVContoller.tableView setSeparatorColor:[UIColor clearColor]];  


Solution:14

You can use:

(instancetype)init {  return [[YourSubclassOfTableView alloc] initWithStyle:UITableViewStyleGrouped];  }  


Solution:15

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »