2011-01-05 106 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似乎當您返回到的tableview是空的。如果你從其他地方修改cityListArray我建議叫[tableView reloadData]viewWillAppear

0

耶它的一個好主意,再重裝一次它的顯示錶中的數據,這樣可以確保數據的正確性。還有一兩件事我想指出的是,如果你有像空格字符的城市名稱或引用它總是一個好主意,以躲避URL字符串,如果沒有你可能會得到意想不到的效果。