1

以下に示すように、「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];
}
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

静的配列にこれらすべてを入力してみませんか?

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

その後、これを返します。

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

テーブルの各行のデータを保持する適切なデータ構造が必要です。この場合、辞書の配列(または同様のもの)の配列が必要です。トップレベルの配列には、セクションごとに1つのエントリがあります。セクション配列には、セクションの行ごとに1つのエントリがあります。これらの各エントリは、行のデータを表す辞書またはカスタムオブジェクトになります。これには、行のデータに関するタイトルやその他の情報が含まれます。

このデータ構造は、「cellForRowAtIndexPath」と「didSelectRowsAtIndexPath」の両方に使用されます。indexPathに基づいて、ディクショナリまたはカスタムクラスを抽出します。

したがって、「didSelectRowAtIndexPath」全体が数行のコードになります。

于 2012-10-10T05:45:49.043 に答える
0

if-elseを回避するためにswitchcaseを使用できますが、これらの都市名をテーブルビューに表示し、その名前をcityControllerに送信する選択に基づいていますか?はいの場合、これらの名前を表示する方法のために、switchcaseを使用する必要はありません配列を使用すると、同じように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 に答える
0

@Wayne Hartmanに同意します。forループは必要ありません。すべてのデータの配列が必要なだけですが、cellForRowAtIndexPathは、セルが生成(またはデキュー)されるたびに呼び出され、次のように配列からタイトルをロードするように指示した場合に呼び出されます。

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

セルタイトルをハードコーディングするよりもはるかにクリーンになります。

于 2012-10-10T05:48:11.493 に答える