我想知道是否有办法缩短我的 CountryViewController.m 因为每次添加新国家时我都会添加另一个 else if 语句。
我正在使用一个 UITableViews 来推送一个新的表格视图。
RootViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
ASIA = [[NSMutableArray alloc] init];
EU = [[NSMutableArray alloc] init];
[ASIA addObject: @"China"];
[ASIA addObject: @"Japan"];
[ASIA addObject: @"Korea"];
[EU addObject: @"France"];
[EU addObject: @"Italy"];
[EU addObject: @"Switzerland"];
self.title = @"Continent";
}
CountryViewController.m
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear: animated];
if ([self.title isEqualToString: @"China"]) {
_listOfCities = [[NSMutableArray alloc] init];
[_listOfCities addObject: @"Beijing"];
[_listOfCities addObject: @"Hong Kong"];
}
else if ([self.title isEqualToString: @"Japan"]){
_listOfCities = [[NSMutableArray alloc] init];
[_listOfCities addObject: @"Tokyo"];
}
else if ([self.title isEqualToString: @"Korea"]){
_listOfCities = [[NSMutableArray alloc] init];
[_listOfCities addObject: @"Seoul"];
}
else if ([self.title isEqualToString: @"France"]){
_listOfCities = [[NSMutableArray alloc] init];
[_listOfCities addObject: @"Paris"];
[_listOfCities addObject: @"Versailles"];
}
else if ([self.title isEqualToString: @"Italy"]){
_listOfCities = [[NSMutableArray alloc] init];
[_listOfCities addObject: @"Rome"];
}
else if ([self.title isEqualToString: @"Switzerland"]){
_listOfCities = [[NSMutableArray alloc] init];
[_listOfCities addObject: @"Bern"];
}
[self.tableView reloadData];
}