ios - Getting this Error in Tutorial App: index 0 beyond bounds for empty array -
please note new edit!!!!
so trying create app loads annotation array , shows them on map...then when select location in uitable on left zooms location on map on right. below code loading annotations, setting number of sections (to viewed in uitable), setting number of rows in each section , writing annotation names uitable can used selector.
problem: app runs uitable on left (to used select locations) shows nothing in hotels section, shows word "empty" in aerialview section, , shows 1 location in streets section remaining rows in streets section blank.
so annotation names not being loaded uitable properly. ideas?
-(void) loadoutannotations { cllocationcoordinate2d workingcoordinate; //***********hotels************************** workingcoordinate.latitude = 40.763856; workingcoordinate.longitude = -73.973034; icodeblogannotation *hotel1 = [[icodeblogannotation alloc] initwithcoordinate:workingcoordinate]; [hotel1 settitle:@"hotel 1"]; [hotel1 setsubtitle:@"this hotel #1"]; [hotel1 setannotationtype:icodeblogannotationtypehotels]; [mapview addannotation:hotel1]; workingcoordinate.latitude = 50.763856; workingcoordinate.longitude = -83.973034; icodeblogannotation *hotel2 = [[icodeblogannotation alloc] initwithcoordinate:workingcoordinate]; [hotel1 settitle:@"hotel 2"]; [hotel1 setsubtitle:@"this hotel #2"]; [hotel1 setannotationtype:icodeblogannotationtypehotels]; [mapview addannotation:hotel2]; workingcoordinate.latitude = 60.763856; workingcoordinate.longitude = -93.973034; icodeblogannotation *hotel3 = [[icodeblogannotation alloc] initwithcoordinate:workingcoordinate]; [hotel1 settitle:@"hotel 3"]; [hotel1 setsubtitle:@"this hotel #3"]; [hotel1 setannotationtype:icodeblogannotationtypehotels]; [mapview addannotation:hotel3]; workingcoordinate.latitude = 10.763856; workingcoordinate.longitude = -23.973034; icodeblogannotation *hotel4 = [[icodeblogannotation alloc] initwithcoordinate:workingcoordinate]; [hotel1 settitle:@"hotel 4"]; [hotel1 setsubtitle:@"this hotel #4"]; [hotel1 setannotationtype:icodeblogannotationtypehotels]; [mapview addannotation:hotel4]; workingcoordinate.latitude = 30.763856; workingcoordinate.longitude = -43.973034; icodeblogannotation *hotel5 = [[icodeblogannotation alloc] initwithcoordinate:workingcoordinate]; [hotel1 settitle:@"hotel 5"]; [hotel1 setsubtitle:@"this hotel #5"]; [hotel1 setannotationtype:icodeblogannotationtypehotels]; [mapview addannotation:hotel5]; // *****************aerials***************************** workingcoordinate.latitude = 25.763856; workingcoordinate.longitude = -15.973034; icodeblogannotation *aerial1 = [[icodeblogannotation alloc] initwithcoordinate:workingcoordinate]; [hotel1 settitle:@"aerial 1"]; [hotel1 setsubtitle:@"this aerial #1"]; [hotel1 setannotationtype:icodeblogannotationtypeaerialshots]; [mapview addannotation:aerial1]; workingcoordinate.latitude = 35.763856; workingcoordinate.longitude = -25.973034; icodeblogannotation *aerial2 = [[icodeblogannotation alloc] initwithcoordinate:workingcoordinate]; [hotel1 settitle:@"aerial 2"]; [hotel1 setsubtitle:@"this aerial #2"]; [hotel1 setannotationtype:icodeblogannotationtypeaerialshots]; [mapview addannotation:aerial2]; workingcoordinate.latitude = 45.763856; workingcoordinate.longitude = -35.973034; icodeblogannotation *aerial3 = [[icodeblogannotation alloc] initwithcoordinate:workingcoordinate]; [hotel1 settitle:@"aerial 3"]; [hotel1 setsubtitle:@"this aerial #3"]; [hotel1 setannotationtype:icodeblogannotationtypeaerialshots]; [mapview addannotation:aerial3]; workingcoordinate.latitude = 55.763856; workingcoordinate.longitude = -45.973034; icodeblogannotation *aerial4 = [[icodeblogannotation alloc] initwithcoordinate:workingcoordinate]; [hotel1 settitle:@"aerial 4"]; [hotel1 setsubtitle:@"this aerial #4"]; [hotel1 setannotationtype:icodeblogannotationtypeaerialshots]; [mapview addannotation:aerial4]; workingcoordinate.latitude = 65.763856; workingcoordinate.longitude = -55.973034; icodeblogannotation *aerial5 = [[icodeblogannotation alloc] initwithcoordinate:workingcoordinate]; [hotel1 settitle:@"aerial 5"]; [hotel1 setsubtitle:@"this aerial #5"]; [hotel1 setannotationtype:icodeblogannotationtypeaerialshots]; [mapview addannotation:aerial5]; // *****************streets***************************** workingcoordinate.latitude = 27.763856; workingcoordinate.longitude = -37.973034; icodeblogannotation *street1 = [[icodeblogannotation alloc] initwithcoordinate:workingcoordinate]; [hotel1 settitle:@"street 1"]; [hotel1 setsubtitle:@"this street #1"]; [hotel1 setannotationtype:icodeblogannotationtypestreets]; [mapview addannotation:street1]; workingcoordinate.latitude = 37.763856; workingcoordinate.longitude = -47.973034; icodeblogannotation *street2 = [[icodeblogannotation alloc] initwithcoordinate:workingcoordinate]; [hotel1 settitle:@"street 2"]; [hotel1 setsubtitle:@"this street #2"]; [hotel1 setannotationtype:icodeblogannotationtypestreets]; [mapview addannotation:street2]; workingcoordinate.latitude = 57.763856; workingcoordinate.longitude = -67.973034; icodeblogannotation *street3 = [[icodeblogannotation alloc] initwithcoordinate:workingcoordinate]; [hotel1 settitle:@"street 3"]; [hotel1 setsubtitle:@"this street #3"]; [hotel1 setannotationtype:icodeblogannotationtypestreets]; [mapview addannotation:street3]; workingcoordinate.latitude = 77.763856; workingcoordinate.longitude = -87.973034; icodeblogannotation *street4 = [[icodeblogannotation alloc] initwithcoordinate:workingcoordinate]; [hotel1 settitle:@"street 4"]; [hotel1 setsubtitle:@"this street #4"]; [hotel1 setannotationtype:icodeblogannotationtypestreets]; [mapview addannotation:street4]; workingcoordinate.latitude = 87.763856; workingcoordinate.longitude = -97.973034; icodeblogannotation *street5 = [[icodeblogannotation alloc] initwithcoordinate:workingcoordinate]; [hotel1 settitle:@"street 5"]; [hotel1 setsubtitle:@"this street #5"]; [hotel1 setannotationtype:icodeblogannotationtypestreets]; [mapview addannotation:street5]; } - (icodeblogannotationview *)mapview:(mkmapview *)mapview viewforannotation:(id<mkannotation>)annotation { icodeblogannotationview *annotationview = nil; icodeblogannotation *myannotation = (icodeblogannotation *)annotation; if(myannotation.annotationtype == icodeblogannotationtypehotels) { nsstring *identifier = @"hotels"; icodeblogannotationview *newannotationview = (icodeblogannotationview*)[self.mapview dequeuereusableannotationviewwithidentifier:identifier]; if (newannotationview == nil) { newannotationview = [[[icodeblogannotationview alloc] initwithannotation:myannotation reuseidentifier:identifier]autorelease]; } annotationview = newannotationview; } else if(myannotation.annotationtype == icodeblogannotationtypeaerialshots) { nsstring *identifier = @"aerialshots"; icodeblogannotationview *newannotationview = (icodeblogannotationview*)[self.mapview dequeuereusableannotationviewwithidentifier:identifier]; if (newannotationview == nil) { newannotationview = [[[icodeblogannotationview alloc] initwithannotation:myannotation reuseidentifier:identifier]autorelease]; } annotationview = newannotationview; } else if(myannotation.annotationtype == icodeblogannotationtypestreets) { nsstring *identifier = @"streets"; icodeblogannotationview *newannotationview = (icodeblogannotationview*)[self.mapview dequeuereusableannotationviewwithidentifier:identifier]; if (newannotationview == nil) { newannotationview = [[[icodeblogannotationview alloc] initwithannotation:myannotation reuseidentifier:identifier]autorelease]; } annotationview = newannotationview; } [annotationview setenabled:yes]; [annotationview setcanshowcallout:yes]; return annotationview; } #pragma mark tableview datasource methods - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 3; } - (nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section { if (section == icodeblogannotationtypehotels) { return @"hotels"; } else if (section == icodeblogannotationtypeaerialshots) { return @"aerialshots"; } else if (section == icodeblogannotationtypestreets) { return @"streets"; } return nil; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return 5; } - (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]; } // here creating array object empty (it has no object in it) nsmutablearray *annotations = [[nsmutablearray alloc] init]; if(indexpath.section == 0) { for(icodeblogannotation *annotation in [mapview annotations]) { if([annotation annotationtype] == icodeblogannotationtypehotels) { // here line add objects array [annotations addobject:annotation]; } } if ([annotations count] > indexpath.row) { cell.textlabel.text = [[annotations objectatindex:indexpath.row] title]; } else { cell.textlabel.text = @"empty"; } } else if (indexpath.section == 1) { for(icodeblogannotation *annotation in [mapview annotations]) { if([annotation annotationtype] == icodeblogannotationtypeaerialshots) { [annotations addobject:annotation]; } } if ([annotations count] > indexpath.row) { cell.textlabel.text = [[annotations objectatindex:indexpath.row] title]; } else { cell.textlabel.text = @"empty"; } } else if (indexpath.section == 2) { for(icodeblogannotation *annotation in [mapview annotations]) { if([annotation annotationtype] == icodeblogannotationtypestreets) { [annotations addobject:annotation]; } } if ([annotations count] > indexpath.row) { cell.textlabel.text = [[annotations objectatindex:indexpath.row] title]; } else { cell.textlabel.text = @"empty"; } } return cell; }
when create object nsmutablearray *annotations = [[nsmutablearray alloc] init];
exists, empty. is, has no objects in it. you're adding objects array in line: [annotations addobject:annotation];
, may never to. is, you'll end adding many objects there annotation
s in [mapview annotations]
of correct annotationtype
.
you blindly access specific element of array without checking see if there enough.
at least, should replace line:
cell.textlabel.text = [[annotations objectatindex:indexpath.row] title];
with this:
if ([annotations count] > indexpath.row) { cell.textlabel.text = [[annotations objectatindex:indexpath.row] title]; } else { cell.textlabel.text = @"empty"; }
better, should change implementation of
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section;
to make sure never there more rows in section there annotations put rows.
Comments
Post a Comment