为什么我的iPhone应用程序崩溃了?

时间:2011-01-05 12:12:40

标签: 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];
    self.title=@"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){
    url=@"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:

2 个答案:

答案 0 :(得分:1)

当您返回到tableview时,数组cityListArray似乎为空。如果您从其他地方修改cityListArray,我建议您在[tableView reloadData]中致电viewWillAppear

答案 1 :(得分:0)

是的,再次显示表数据是一个好主意,这样你可以确保数据是正确的。我想指出的另一件事是,如果你的城市名称包含空格或引号等字符,那么它总是一个好主意来逃避url字符串,否则你可能会得到意想不到的结果。