2011-01-05 11 views
0

私のアプリケーションがクラッシュする。それは、すべての都市のデータを読み込んでいて、詳細なビューコントローラを表示しているときにクリックします。 コントローラから戻ってきて、別の都市を選択すると、アプリケーションがクラッシュします。私のiPhoneアプリケーションがクラッシュするのはなぜですか?

私は自分のコードを貼り付けています。

#import "CityNameViewController.h" 
#import "Cities.h" 
#import "XMLParser.h" 
#import "PartyTemperature_AppDelegate.h" 
#import "CityEventViewController.h" 

@implementation CityNameViewController 
//@synthesize aCities; 
@synthesize appDelegate; 
@synthesize currentIndex; 
@synthesize aCities; 
/* 
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { 
     // Custom initialization 
    } 
    return self; 
} 
*/ 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [email protected]"Cities"; 
    appDelegate=(PartyTemperature_AppDelegate *)[[UIApplication sharedApplication]delegate]; 
} 
/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
     // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
     // Return the number of rows in the section. 

    return [appDelegate.cityListArray count]; 
} 
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 95.0f; 
} 

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; 
    cell.textLabel.textColor = [[[UIColor alloc] initWithRed:0.2 green:0.2 blue:0.6 alpha:1] autorelease]; 
    cell.detailTextLabel.textColor = [UIColor blackColor]; 
    cell.detailTextLabel.font=[UIFont systemFontOfSize:10]; 
    if (indexPath.row %2 == 1) { 
     cell.backgroundColor = [[[UIColor alloc] initWithRed:0.87f green:0.87f blue:0.87f alpha:1.0f] autorelease]; 
    } else { 
     cell.backgroundColor = [[[UIColor alloc] initWithRed:0.97f green:0.97f blue:0.97f alpha:1.0f] autorelease]; 
    } 
} 

    // 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:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     cell.selectionStyle= UITableViewCellSelectionStyleBlue; 
      //  cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; 
    cell.backgroundColor=[UIColor blueColor]; 
    } 
// aCities=[appDelegate.cityListArray objectAtIndex:indexPath.row]; 
// cell.textLabel.text=aCities.city_Name; 
    cell.textLabel.text=[[appDelegate.cityListArray objectAtIndex:indexPath.row]city_Name]; 
    return cell; 
} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    //http://compliantbox.com/party_temperature/citysearch.php?city=Amsterdam&latitude=52.366125&longitude=4.899171 
    NSString *url; 
    aCities=[appDelegate.cityListArray objectAtIndex:indexPath.row]; 
    if ([appDelegate.cityListArray count]>0){ 
    [email protected]"http://compliantbox.com/party_temperature/citysearch.php?city="; 
    url=[url stringByAppendingString:aCities.city_Name]; 
    url=[url stringByAppendingString:@"&latitude=52.366125&longitude=4.899171"]; 
    NSLog(@"url value is %@",url); 
    [self parseCityName:[[NSURL alloc]initWithString:url]]; 
    } 
} 

-(void)parseCityName:(NSURL *)url{ 
    NSXMLParser *xmlParser=[[NSXMLParser alloc]initWithContentsOfURL:url]; 
    XMLParser *parser=[[XMLParser alloc] initXMLParser]; 
    [xmlParser setDelegate:parser]; 
    BOOL success; 
    success=[xmlParser parse]; 
    if (success) { 
     NSLog(@"Sucessfully parsed"); 
     CityEventViewController *cityEventViewController=[[CityEventViewController alloc]initWithNibName:@"CityEventViewController" bundle:nil]; 
     cityEventViewController.index=currentIndex; 
     [self.navigationController pushViewController:cityEventViewController animated:YES]; 
     [cityEventViewController release]; 
     cityEventViewController=nil; 
    } 
    else { 
     NSLog(@"Try it Idoit"); 
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Alert!" message:@"Event Not In Radius" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
    } 


} 
- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [aCities release]; 
    [super dealloc]; 
} 
@end 

とエラーが

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 1 beyond bounds for empty array' 
*** Call stack at first throw: 

答えて

1

アレイcityListArrayでは、あなたがテーブルビューに戻ったときに、空のようです。あなたが他の場所からcityListArrayを変更した場合、私はviewWillAppear[tableView reloadData]を呼び出すことをお勧めしたいです。

0

ええ、その再び表示一度テーブルデータを再ロードすることをお勧め、あなたはデータが正しいことを確認することができ、そのよう。私が指摘したいもう一つは、あなたがスペースのような文字で都市名を持っている場合であるか、予期しない結果を得る可能性がありますいない場合は、URL文字列をエスケープするために、その常に良いアイデアを引用しています。

関連する問題