【发布时间】:2012-10-10 05:38:49
【问题描述】:
如下所示,我有两个部分“A-M”和“N-Z”。我计划添加几个城市,但我的 didSelectRowAtIndexPath 代码似乎很长。
什么循环,你如何实现它,这样我就不必添加很多 else if。
- (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];
}
【问题讨论】: