1

我有两个部分“AM”和“NZ”,如下所示。我计划添加几个城市,但看起来我的 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];
}
4

5 回答 5

2

我会采取不同的方法来编写代码。因为您在其中对这些城市名称进行了硬编码,所以您将更加难以添加/删除/以其他方式维护您的代码。在最基本的层面上,您可以在表格视图中保留一系列城市。这将允许您在不更改代码的情况下更改数据。

标题:

@interface MyTableViewController : UITableViewController

@property (nonatomic, strong) NSArray *cities;

@end

执行:

@implementation MyTableViewController

// other code

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *city = self.cities[indexPath.row];

    MyCityController *controller = // Init code;
    controller.title = city;

    NSLog(@"Selected city: %@", city);
}

// Other code

@end

您的数据源应该类似地实现:

- (UITableViewCell *)tableView:(UITableView *) cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CELL_ID = @"CITIES";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_ID];

    cell.textLabel.text = self.cities[indexPath.row];

    // Other setup code

    return cell;
}

您可以使用硬编码值延迟加载它们:

- (void)cities {
    if (_cities == nil) {
        _cities = @[@"Bologna", @"Florence", @"Milan", @"Naples", @"Rome"];
    }

    return _cities;
}

或者从 plist 中加载它们viewDidLoad

- (void)viewDidLoad {
    if (!self.cities) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"cities" ofType:@"plist"];
        NSDictionary *cityDict = [NSDictionary dictionaryWithContentsOfFile:path];
        self.cities = [cityDict objectForKey:@"cities"];
    }
}
于 2012-10-10T05:44:31.963 回答
0

我同意@Wayne Hartman 的观点,你不想要一个 for 循环,你只想要一个包含所有数据的数组。每次生成(或出列)单元格时都会调用 cellForRowAtIndexPath 并且如果您只是告诉它从数组中加载其标题,例如

[cell.textLabel setText:[citiesInItaly objectAtIndex:indexPath.row] 

这将比硬编码单元格标题更干净。

于 2012-10-10T05:48:11.493 回答
0

你为什么不用所有这些填充静态数组可能是这样的

NSArray *strings = @[ @[ @"City1", @"B", @"C" ], @[ @"D", @"E", @"F" ]];

之后就返回这个。

Strings[sectionindex][roeindex];
于 2012-10-10T05:43:37.687 回答
0

您应该有一个适当的数据结构来保存表中每一行的数据。在这种情况下,您应该有一个字典数组(或类似的东西)。顶级数组每个部分将有一个条目。对于节中的每一行,节数组将有一个条目。这些条目中的每一个都将是字典或一些表示该行数据的自定义对象。这将包括标题和有关该行数据的任何其他内容。

此数据结构将用于“cellForRowAtIndexPath”和“didSelectRowsAtIndexPath”。根据 indexPath 您提取字典或自定义类。

因此,您的整个 'didSelectRowAtIndexPath' 只需几行代码。

于 2012-10-10T05:45:49.043 回答
0

您可以使用 switch case 来避免 if-else。但是您是否在 tableview 中显示这些城市名称并基于将名称发送到 cityController 的选择?如果是,那么也不需要使用 switch case,因为您显示这些名称的方式使用数组,同样您可以使用 indexPath 发送该名称。

使用开关盒:

switch(indexPath.section)
{
  case 0:
        {
           switch(indexPath.row)
           {
             case 0:
              stmt;
              break;

              case 1:
              stmt;
              break;
           }
        }
  same as case0... 
} 
于 2012-10-10T05:46:53.037 回答