0

I'm a beginner at this and I'm trying to create an app in which you can select from several options. Therefore, I have a table view. Depending on which cell was tapped, another screen should appear with a second table view where I can checkmark things. Right now, I have one view controller in which I'm creating the first table view. How do I get to the second table view and how does it display the other array. Here is the code so far...

@interface ViewController (){

  NSArray *genres;
  NSArray *images;
  NSMutableArray *array1;
  NSMutableArray *array2;
  NSMutableArray *array3;
}
@end

@implementation ViewController

@synthesize tableView;

(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
  self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  if (self) {
    // Custom initialization
  }
  return self;
}

- (void)viewDidLoad
{
  [super viewDidLoad];
  // Do any additional setup after loading the view.

  self.title = @"Genres";

  // Arrays must be in same order for image and title correspondance

  genres = [[NSArray alloc] initWithObjects:
            @"a1",
            @"a2",
            @"a3",
            nil];

  images = [[NSArray alloc] initWithObjects:
            @"1.jpeg",
            @"2.jpeg",
            @"3.jpeg",
            nil];

  array1 = [[NSMutableArray alloc] initWithObjects:
            @"1.1",
            @"1.2",
            @"1.3",
            nil];

  array2 = [[NSMutableArray alloc] initWithObjects:
            @"2.1",
            @"2.2",
            @"2.3",
            nil];

  array3 = [[NSMutableArray alloc] initWithObjects:
            @"3.1",
            @"3.2",
            @"3.3",
            nil];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger) numberOfSectionsInTableView:(UITableView *) tableView{
  return 1;
}

- (NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section{
  return genres.count;
}

//Customize the apperance of table view cells

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     
*)indexPath
{
  static NSString *CellIdentifier = @"Cell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  
      reuseIdentifier:CellIdentifier];
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
      }
  }

//Configure Cell

cell.textLabel.text = [genres objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[images objectAtIndex:indexPath.row]];
return cell;
}

#pragma mark -
#pragma mark Table view delegate

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

  NSArray *selectedArray = [[NSArray alloc] init];

  switch (indexPath.row) {
    case 0:
        selectedArray = array1;
        break;
    case 1:
        selectedArray = array2;
        break;
    case 2:
        selectedArray = array3;
        break;        
    default:
        break;
  }
}
Tim
  • 8,932
  • 4
  • 43
  • 64
  • No need to call `NSArray *selectedArray = [[NSArray alloc] init];` you can just assign the pointers like `NSArray *selectedArray = nil;` `selectedArray = array1;` – Alex Peda Aug 06 '14 at 09:18
  • Do you use storyboards and define the second view controller there? Or you define view controllers from code only? – Alex Peda Aug 06 '14 at 09:19

1 Answers1

1

You can 1) create second view controller from code and set array like argument or 2) use segue interface:

Try to follow this complete answer: https://stackoverflow.com/a/9736559/2429147

Also this link may be helpful regarding to work with segues and storyboards: http://agilewarrior.wordpress.com/2012/01/25/how-segue-in-ios-and-pass-data-from-one-viewcontroller-to-another/

Community
  • 1
  • 1
Alex Peda
  • 4,210
  • 3
  • 22
  • 31