views:

5208

answers:

5

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. Thanks.

A: 

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.

drewh
The compiler complains that this is a readonly property.
nevan
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.
nevan
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.
drewh
+6  A: 

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

myTVContoller = [[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];
Dimitris
As stated it is not a property that can be changed. One grouped always grouped.
Corey Floyd
+8  A: 

You can do the following:

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

Lior
THIS IS THE REAL SOLUTION! THANKS. The accepted answer is wrong, at least for OS 3.0+
Digital Robot
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`.
nevan
A: 

doesn't work

I am not using a XIB

vivian
A: 

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

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