views:

59

answers:

2

This is a little side project I'm working on for fun.

I begin by initializing with

- (void)viewDidLoad {

[super viewDidLoad];

rootArray = [[NSMutableArray alloc] initWithObjects:@"Earth",@"Mars",@"Pluto",nil];
moonArray = [[NSMutableArray alloc] initWithObjects:@"Clavius",@"Galileo",@"Ptolamy",nil];
marsArray = [[NSMutableArray alloc] initWithObjects:@"Phobos",@"Delmos",nil];
plutoArray = [[NSMutableArray alloc] initWithObjects:@"Charon",@"Nix",@"Hydra",nil];

self.planetList = rootArray

I just make 5 arrays, initializing planetList with rootArray. From there, I wish to use these next arrays to create table views. planetList will be the main list when the app starts, and the other 3 will be the arrays called on for the next views. So, I then have this bit of code to handle this event:

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


MoonViewController *myMoonViews = [[MoonViewController alloc] initWithNibName:@"MoonViewController" bundle:nil];

UITableViewCell *targetCell = [tableView cellForRowAtIndexPath:indexPath];

if([targetCell.textLabel.text isEqualToString:@"Earth"])
{
    myMoonViews.planetList = moonArray;
    myMoonViews.title = @"First Title";
}

else if([targetCell.textLabel.text isEqualToString:@"Mars"])
{
    myMoonViews.planetList = marsArray;
    myMoonViews.title = @"Second Title";
}

else if([targetCell.textLabel.text isEqualToString:@"Pluto"])
{
    myMoonViews.planetList = plutoArray;
    myMoonViews.title = @"Third Title";
}

 // ...
 // Pass the selected object to the new view controller.
[self.navigationController pushViewController:myMoonViews animated:YES];
[myMoonViews release];
}

I have another file the next view, MoonViewController, that uses the same code from my original ViewController (which works just fine). However I don't know where I am going wrong.

Side Notes:

  • planetList is a NSMutableArray
  • I am not sure what to put in the viewDidLoad of my MoonViewController.m (If this is a giant issue I was not told that when I was taught this.)
  • If there is more code needed to be seen I will supply.

Thank you very much for any help!

EDIT1: Sorry I definitely should have thought to put this. The error is one this line of the second code block:

if([targetCell.textLabel.text isEqualToString:@"Earth"])

EDIT2: The MoonViewController.m functions:

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


// Customize the appearance 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] autorelease];
}

// Configure the cell.
cell.textLabel.text = [planetList objectAtIndex: indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;

}

A: 

There is nothing obscenely wrong with the code you've posted. Therefore, your problem may be somewhere in MoonViewController. Can you post the tableView methods?

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Jordan
Posted edit2. Those are the functions.
Inanepenguin
Well you're missing something somewhere, but I can't tell from the code you posted. It would be helpful if you stepped through the code to find out exactly where it is crashing. Let me know what happens after you've done so.
Jordan
Crud, I'm sorry I meant to put that too. When I click on the button "Earth" (The app starts with a table view with 3 cells, "Earth", "Mars", and "Pluto"). It crashes and the debug mode points to the line that I posted (The if statement). If there is any extra info I can provide through Xcode (crash report or anything) let me know.
Inanepenguin
What is the exact error? Post it.
Jordan
A: 

This won't solve your problem, but count is not a property. It works, but you should actually use [planetList count] instead.

Also, I don't think you need to actually look at the value of textLabel.text. You can simply check your array:

if([[moonArray objectAtIndex: indexPath.row] isEqualToString:@"Earth"])
Joseph Tura