Mr. Pie Guy
Mr. Pie Guy

Reputation: 119

How to change the style of a UITableViewController programmatically in Xcode

I'm trying to change the style of a UITableViewController to grouped. I know you can do this when creating a new table view, but I have a class that extends UITableViewController, so I don't need to make a new table view. Here's my code:

#import "DetailViewController.h"
#import "NSArray-NestedArrays.h"

@implementation DetailViewController

@synthesize steak, sectionNames, rowControllers, rowKeys, rowLabels;

- (void)viewDidLoad {
    sectionNames = [[NSArray alloc] initWithObjects:[NSNull null], NSLocalizedString(@"General", @"General"), nil];
    rowLabels = [[NSArray alloc] initWithObjects:
             [NSArray arrayWithObjects:NSLocalizedString(@"Steak Name", @"Steak Name"), nil],
             [NSArray arrayWithObjects:NSLocalizedString(@"Steak Wellness", @"Steak Wellness"), NSLocalizedString(@"Steak Type", @"Steak Type"), NSLocalizedString(@"Other", @"Other"), nil]
             , nil];
    rowKeys = [[NSArray alloc] initWithObjects:
           [NSArray arrayWithObjects:@"steakName", nil],
           [NSArray arrayWithObjects:@"steakWellness", @"steakType", @"other", nil]
           , nil];


    // TODO: Populate row controllers array

    [super viewDidLoad];

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [sectionNames count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    id theTitle = [sectionNames objectAtIndex:section];
    if ([theTitle isKindOfClass:[NSNull class]]) {
        return nil;
    }
    return theTitle;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [rowLabels countOfNestedArray:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"SteakCellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
    }

    NSString *rowKey = [rowKeys nestedObjectAtIndexPath:indexPath];
    NSString *rowLabel = [rowLabels nestedObjectAtIndexPath:indexPath];

    cell.detailTextLabel.text = rowKey;
    cell.textLabel.text = rowLabel;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // TODO: Push editing controller onto the stack
} 
@end 

Upvotes: 4

Views: 8861

Answers (7)

Novarg
Novarg

Reputation: 7450

You can not just change the style of an UITableView. So you only have 2 options:

  1. Make another UITableView which is grouped
  2. Use custom cells

Upvotes: 2

yoAlex5
yoAlex5

Reputation: 34255

iOS style for UITableViewController

Swift version

final lass CustomTableViewController: UITableViewController {
    required init(customParam: String) {        
        super.init(style: .grouped)
    }
}

Upvotes: 0

pengshuai liang
pengshuai liang

Reputation: 41

- (instancetype)init
{
   self = [super initWithStyle:UITableViewStyleGrouped];
   if (self) {
   }
   return self;
}

Upvotes: 4

howeguo
howeguo

Reputation: 11

You can create a new tableview to overwrite UITableviewController's tableview like this:

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

Upvotes: 1

Bajaj
Bajaj

Reputation: 869

simply buddy to allocate set the frame and style of table ..example code write down.

[tableObject initWithFrame:CGRectMake(x,y,width,height) style:UITableViewStyleGrouped];

Upvotes: -1

Freddy
Freddy

Reputation: 2279

You would take care of this when you instantiated your view controller.

For example, to instantiate a normal UITableViewController you would do the following.

UITableViewController *tblCtr = [[UITableViewController alloc]initWithStyle:UITableViewStyleGrouped];

Therefore, if you have extended UITableViewController then your init code should take care of this.

MyCustomTableViewController *mctvc = [[MyCustomTableViewController alloc]initWithStyle:UITableViewStyleGrouped];

To achieve this you will need to implement this method in your .m file. Below is an example of what your header and implementation file should contain for instantiation.

Header

@interface MyCustomTableViewController : UITableViewController
{
  -(id)initWithStyle:(UITableViewStyle)style;
}

Implementation

@implementation MyCustomTableViewController

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

   if(self)
   {
     ...
     return self;
   }
   return nil;
 }
 @end

When you call [super initWithStyle:style] the code provided by apple will take care of building the tableview for you with the requested view style.

Upvotes: 3

Cliff Ribaudo
Cliff Ribaudo

Reputation: 9039

Not following? What do you mean by you don't have to "make a new table view"?? You still have to instantiate one.

Either you created one already and it has the style you want, or you have to instantiate a new one and set the property on it.

tableView.style is READONLY. So you can't change the style of an existing one. You are going to have to do something like:

[MyTableViewSubClass initWithFrame:aFrame style:UITableViewStyleGrouped];

Upvotes: 2

Related Questions