I am trying to make different table views appear on a view when a user pushes a button on a segmented controller my approach was to create two different instances for each table and then hide one of the tables when a user presses a button. my code below compiles but no table appears on my simulator.
heres my header and .m files
heres my header and .m files
Code:
#import <UIKit/UIKit.h>
@interface SettingsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
UITableView *ivMainTableView;
NSMutableArray *ivContentsList;
UITableView *secondTableView;
NSMutableArray *secondContentsLists;
UIView *currentTable;
UISwitch *switchCtl;
UISegmentedControl *buttonBarSegmentedControl;
}
// Properties
@property (nonatomic, retain, readonly) UISwitch *switchCtl;
@property (nonatomic, retain) IBOutlet UITableView *mainTableView;
@property (nonatomic, retain) NSMutableArray *contentsList;
@property (nonatomic, retain) IBOutlet UISegmentedControl *buttonBarSegmentedControl;
@property (nonatomic, retain) IBOutlet UITableView *secondTableView;
@property (nonatomic, retain) NSMutableArray *secondContentsLists;
@property (nonatomic, retain) UIView *currentTable;
- (IBAction)togglePickers:(id)sender; // for changing between UIPickerView, UIDatePickerView and custom picker
// Notification Handlers
// UI Response
// Other
@end
Code:
//
// ArrayViewController.m
// SlickArraySectionTableViews
//
// Created by Brian Slick on 2/2/11.
// Copyright 2011 BriTer Ideas LLC. All rights reserved.
//
#import "SettingsViewController.h"
// Models and other global
// Sub-controllers
@implementation SettingsViewController
#pragma mark -
#pragma mark Synthesized Properties
@synthesize mainTableView = ivMainTableView;
@synthesize contentsList = ivContentsList;
@synthesize switchCtl;
@synthesize buttonBarSegmentedControl;
@synthesize secondTableView;
@synthesize secondContentsLists;
@synthesize currentTable;
#pragma mark -
#pragma mark Dealloc and Memory Methods
- (CGRect)pickerFrameWithSize:(CGSize)size
{
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
CGRect pickerRect = CGRectMake( 0.0,
screenRect.size.height - 42.0 - size.height,
size.width,
size.height);
return pickerRect;
}
- (void)didReceiveMemoryWarning
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
[super didReceiveMemoryWarning];
NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}
- (void)setView:(UIView *)aView
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
if (!aView) // view is being set to nil
{
}
[super setView:aView];
NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}
#pragma mark -
#pragma mark Custom Getters and Setters
#pragma mark -
#pragma mark Initialization and UI Creation Methods
- (id)init
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
return [self initWithNibName:nil bundle:nil];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
}
NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
return self;
}
#pragma mark -
#pragma mark UIViewController Methods
- (void)createMain
{
self.mainTableView.backgroundColor = [UIColor whiteColor];
NSArray *firstSection = [NSArray arrayWithObjects:@"Local Weather", @"Our Menu",@"Hotel Entertainment", nil];
NSArray *secondSection = [NSArray arrayWithObjects:@"Map",@"Hotel History",@"Dining Room",@"Hotel Bars",@"Book Now",@"Swimming Pool", nil];
NSArray *thirdSection = [NSArray arrayWithObjects:@"About",@"Bouncing Beers", nil];
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:firstSection, secondSection, thirdSection, nil];
[self setContentsList:array];
[array release], array = nil;
// position the picker at the bottom
ivMainTableView = [[UITableView alloc] initWithFrame:CGRectZero];
ivMainTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
CGSize pickerSize = [ivMainTableView sizeThatFits:CGSizeZero];
ivMainTableView.frame = [self pickerFrameWithSize:pickerSize];
// this view controller is the data source and delegate
ivMainTableView.delegate = self;
ivMainTableView.dataSource = self;
// add this picker to our view controller, initially hidden
ivMainTableView.hidden = NO;
[self.view addSubview:ivMainTableView];
}
- (void)createTwo
{
self.secondTableView.backgroundColor = [UIColor redColor];
NSArray *firstsSection = [NSArray arrayWithObjects:@"Local GEIGER", @"Our Menu",@"Hotel Entertainment", nil];
NSArray *secondsSection = [NSArray arrayWithObjects:@"Map",@"Hotel History",@"Dining Room",@"Hotel Bars",@"Book Now",@"Swimming Pool", nil];
NSArray *thirdsSection = [NSArray arrayWithObjects:@"About",@"Bouncing Beers", nil];
NSMutableArray *arrays = [[NSMutableArray alloc] initWithObjects:firstsSection, secondsSection, thirdsSection, nil];
[self setSecondContentsLists:arrays];
[arrays release], arrays = nil;
secondTableView = [[UITableView alloc] initWithFrame:CGRectZero];
secondTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
CGSize pickerSize = [secondTableView sizeThatFits:CGSizeZero];
secondTableView.frame = [self pickerFrameWithSize:pickerSize];
// this view controller is the data source and delegate
secondTableView.delegate = self;
secondTableView.dataSource = self;
// add this picker to our view controller, initially hidden
secondTableView.hidden = NO;
[self.view addSubview:secondTableView];
}
- (void)showPicker:(UIView *)picker
{
// hide the current picker and show the new one
if (currentTable)
{
currentTable.hidden = YES;
}
currentTable.hidden = NO;
currentTable = picker; // remember the current picker so we can remove it later when another one is chosen
}
- (IBAction)togglePickers:(id)sender
{
UISegmentedControl *segControl = sender;
switch (segControl.selectedSegmentIndex)
{
case 0: // UIPickerView
{
[self showPicker:ivMainTableView];
break;
}
case 1: // UIDatePicker
{
// start by showing the time picker
// initially set the picker style to "date" format
[self showPicker:secondTableView];
break;
}
}
}
- (void)viewDidLoad
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
[super viewDidLoad];
self.title = NSLocalizedString(@"Information", @"Information");
[self createMain];
[self createTwo];
//self.mainTableView.backgroundColor = [UIColor clearColor];
}
- (void)dealloc
{
[buttonBarSegmentedControl release];
[super dealloc];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// release and set out IBOutlets to nil
self.buttonBarSegmentedControl = nil;
// release all the other objects
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// we support rotation in this view controller
return YES;
}
- (void)viewDidAppear:(BOOL)animated
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
[super viewDidAppear:animated];
NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}
- (void)viewWillDisappear:(BOOL)animated
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
[super viewWillDisappear:animated];
NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}
- (void)viewDidDisappear:(BOOL)animated
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
[super viewDidDisappear:animated];
NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}
#pragma mark -
#pragma mark Notification Handlers
#pragma mark -
#pragma mark UI Response Methods
#pragma mark -
#pragma mark Misc Methods
#pragma mark -
#pragma mark UITableView Datasource Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
NSInteger sections = [[self contentsList] count];
NSInteger sectionsT = [[self secondContentsLists] count];
NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
return sections;
return sectionsT;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
self.mainTableView.sectionHeaderHeight = 40;
NSString *title = nil;
// Return a title or nil as appropriate for the section.
switch (section) {
case 0:
title = @"Hotel Information";
break;
case 1:
title = @"On the Go Information";
break;
case 2:
title = @"Our Website";
break;
default:
break;
}
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 40)];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0, tableView.bounds.size.width - 1,26)] autorelease];
label.text = title;
label.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"section_bar_green.png"]];
label.font = [UIFont fontWithName:@"Futura" size:16];
label.backgroundColor = [UIColor clearColor];
[v addSubview:label];
// [UIColor colorWithPatternImage:[UIImage imageNamed:@"section_bar_green.png"]];;
return v;
}
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 27.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
self.mainTableView.sectionHeaderHeight = 40;
NSString *title = nil;
// Return a title or nil as appropriate for the section.
switch (section) {
case 0:
title = @"Here is some information that is useful to every guest!";
break;
case 1:
title = @"Check out some of this important information regarding this hotel";
break;
case 2:
title = @"Click to see what future updates will hold and when they will be released";
break;
default:
break;
}
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 40)];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0, tableView.bounds.size.width - 1,26)] autorelease];
label.text = title;
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:@"Futura" size:10];
label.backgroundColor = [UIColor clearColor];
[v addSubview:label];
// [UIColor colorWithPatternImage:[UIImage imageNamed:@"section_bar_green.png"]];;
return v;
}
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 27.0;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
NSArray *sectionContents = [[self contentsList] objectAtIndex:section];
NSInteger rows = [sectionContents count];
NSArray *sectionTContents = [[self secondContentsLists] objectAtIndex:section];
NSInteger rowsT = [sectionTContents count];
NSLog(@"rows is: %d", rows);
NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
return rows;
return rowsT;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
NSArray *sectionContents = [[self contentsList] objectAtIndex:[indexPath section]];
NSString *contentForThisRow = [sectionContents objectAtIndex:[indexPath row]];
NSArray *sectionTContents = [[self secondContentsLists] objectAtIndex:[indexPath section]];
NSString *contentTForThisRow = [sectionTContents objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
//NSUInteger row =[indexPath row];
//NSUInteger section = [indexPath indexAtPosition:0];
[[cell textLabel] setText:contentForThisRow];
if ([cell.textLabel.text isEqualToString:@"Local Weather"]) {
[cell.imageView setImage:[UIImage imageNamed:@"weather.png"]];
cell.detailTextLabel.text = @"Weather";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.font = [UIFont fontWithName:@"Futura" size:17];
cell.detailTextLabel.font = [UIFont fontWithName:@"Futura" size:17];
}
if ([cell.textLabel.text isEqualToString:@"Map"]) {
[cell.imageView setImage:[UIImage imageNamed:@"weather.png"]];
cell.detailTextLabel.text = @"Weather";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.font = [UIFont fontWithName:@"Futura" size:17];
cell.detailTextLabel.font = [UIFont fontWithName:@"Futura" size:17];
}
if ([cell.textLabel.text isEqualToString:@"Our Menu"]) {
[cell.imageView setImage:[UIImage imageNamed:@"menu.png"]];
cell.detailTextLabel.text = @"Menu";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.font = [UIFont fontWithName:@"Futura" size:17];
cell.detailTextLabel.font = [UIFont fontWithName:@"Futura" size:17];
}
if ([cell.textLabel.text isEqualToString:@"Hotel Entertainment"]) {
[cell.imageView setImage:[UIImage imageNamed:@"entertain.png"]];
cell.detailTextLabel.text = @"Fun";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.font = [UIFont fontWithName:@"Futura" size:17];
cell.detailTextLabel.font = [UIFont fontWithName:@"Futura" size:17];
}
if ([cell.textLabel.text isEqualToString:@"Hotel History"]) {
[cell.imageView setImage:[UIImage imageNamed:@"history.png"]];
cell.detailTextLabel.text = @"";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.font = [UIFont fontWithName:@"Futura" size:17];
cell.detailTextLabel.font = [UIFont fontWithName:@"Futura" size:17];
}
if ([cell.textLabel.text isEqualToString:@"Dining Room"]) {
[cell.imageView setImage:[UIImage imageNamed:@"dining.png"]];
cell.detailTextLabel.text = @"";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.font = [UIFont fontWithName:@"Futura" size:17];
cell.detailTextLabel.font = [UIFont fontWithName:@"Futura" size:17];
}
if ([cell.textLabel.text isEqualToString:@"Hotel Bars"]) {
[cell.imageView setImage:[UIImage imageNamed:@"bar.png"]];
cell.detailTextLabel.text = @"";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.font = [UIFont fontWithName:@"Futura" size:17];
cell.detailTextLabel.font = [UIFont fontWithName:@"Futura" size:17];
}
if ([cell.textLabel.text isEqualToString:@"Book Now"]) {
[cell.imageView setImage:[UIImage imageNamed:@"book now.png"]];
cell.detailTextLabel.text = @"";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.font = [UIFont fontWithName:@"Futura" size:17];
cell.detailTextLabel.font = [UIFont fontWithName:@"Futura" size:17];
}
if ([cell.textLabel.text isEqualToString:@"Swimming Pool"]) {
[cell.imageView setImage:[UIImage imageNamed:@"pool.png"]];
cell.detailTextLabel.text = @"";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.font = [UIFont fontWithName:@"Futura" size:17];
cell.detailTextLabel.font = [UIFont fontWithName:@"Futura" size:17];
}
if ([cell.textLabel.text isEqualToString:@"About"]) {
[cell.imageView setImage:[UIImage imageNamed:@"banner.png"]];
cell.detailTextLabel.text = @"";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.font = [UIFont fontWithName:@"Futura" size:17];
cell.detailTextLabel.font = [UIFont fontWithName:@"Futura" size:17];
}
if ([cell.textLabel.text isEqualToString:@"Bouncing Beers"]) {
[cell.imageView setImage:[UIImage imageNamed:@"NBall.png"]];
cell.detailTextLabel.text = @"";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.font = [UIFont fontWithName:@"Futura" size:17];
cell.detailTextLabel.font = [UIFont fontWithName:@"Futura" size:17];
}
/*
if ([cell.textLabel.text isEqualToString:@"Push"]) {
//[cell.imageView setImage:[UIImage imageNamed:@"aboutusicon.png"]];
cell.detailTextLabel.text = @"Time";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
*/
NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
return cell;
}
#pragma mark -
#pragma mark UITableView Delegate Methods
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSMutableString *workoutName = [NSMutableString string];
[workoutName setString:[[[tableView cellForRowAtIndexPath:indexPath] textLabel] text]];
[workoutName replaceOccurrencesOfString:@" " withString:@"" options:0 range:NSMakeRange(0, [workoutName length])];
[workoutName appendFormat:@"ViewController"];
id newViewController = [[objc_getClass([workoutName cStringUsingEncoding:NSUTF8StringEncoding]) alloc] init];
[self.navigationController pushViewController:newViewController animated:YES];
NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}
@end
Last edited: