iPhone app crashes when comparing cell.textlabel.text to a string
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:@"MoonV开发者_如何学编程iewController" 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;
}
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
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"])
精彩评论