以下に示すように、「AM」と「NZ」の2つのセクションがあります。いくつかの都市を追加する予定ですが、didSelectRowAtIndexPathのコードは非常に長くなるようです。
どのループで、どのように実装すれば、他に多くのループを追加する必要がなくなります。
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
switch (section) {
case 0:
return @"A-M";
break;
case 1:
return @"N-Z";
break;
default:
break;
}
return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0 && indexPath.row == 0) {
self.cityController.title = @"Bologna";
}
else if (indexPath.section == 0 && indexPath.row == 1) {
self.cityController.title = @"Florence";
}
else if (indexPath.section == 1 && indexPath.row == 0) {
self.cityController.title = @"Naples";
}
else if (indexPath.section == 1 && indexPath.row == 1) {
self.cityController.title = @"Rome";
}
[self.navigationController pushViewController: self.cityController animated: YES];
}