1

AMとNZの2つのセクションがあります。

各セクションに同じ数の都市があれば、問題はないことに気づきました。ただし、同じ数の都市がないと、プログラムがクラッシュします。

エラーは

-[__ NSArrayM objectAtIndex:]:境界を超えたインデックス2 [0 .. 0] '

これは、エラーが生成されたときの私のコードです。

- (void)viewDidLoad
{
        [super viewDidLoad];

        self.title = @"Region";

        self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];

        AM  = [[NSMutableArray alloc] init];
        NZ  = [[NSMutableArray alloc] init];

        [AM    addObject: @"Bologna"];
        [AM    addObject: @"Florence"];
        [AM    addObject: @"Milan"];
        [NZ    addObject: @"Naples"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    switch (section) {
        case 0:
            return [AM count];
            break;

        case 1:
            return [NZ count];
            break;

        default:
            return section;
            break;
    }
}

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        // Configure the cell...

        NSInteger section = [indexPath section];

        switch (section) {
            case 0:
                [cell.textLabel setText: [AM objectAtIndex: [indexPath row]]];
                break;

            case 1:
                [cell.textLabel setText: [NZ objectAtIndex: [indexPath row]]];
                break;

            default:
                break;
        }    
        return cell;
    }


    - (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
    {
        NSString * AMPath    = self.luzonRegion      [indexPath.row];
        NSString * NZPath    = self.visayasRegion    [indexPath.row];

        switch (indexPath.section) {
            case 0:
                cityController.title = luzonRegionPath;
                NSLog(@"Selected city: %@", AMPath);
                break;

            case 1:
                cityController.title = visayasRegionPath;
                NSLog(@"Selected city: %@", NZPath);
                break;

            default:
                break;
        }

        [[self navigationController] pushViewController:cityController animated:YES];
    }
4

2 に答える 2

4

XIBに直接追加した場合UITableView、アレイからのデータをXIBに取り込むには2つの方法があります。必要に応じて、プログラム内またはプログラム内delegatedatasource直接バインドする必要があります。IB

表示するデータをいつ取得するかわからない場合は、データを提供して、データを取得delegatedatasourceたらを指定できますNSArray。あなたがそれにそれを与えるとき、datasourceそれはそのメソッドselfを呼び出すでしょう、それらはそうかもしれません、datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

どちらも、datasourceにデータを入力するために必要なメソッドですUITableView

あなたの質問の唯一の違いは、あなたが自分でテーブルを作成しているということです。したがって、self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];行の実行後、datasourceメソッドを呼び出します。あなたの場合、オブジェクトを作成して追加した後、それを作成する場所を変更する必要があります。また、そのdelegate&を指定する必要がありますdatasource

したがって、ソリューションは次のようになります。

- (void)viewDidLoad
{
    //After creating AM & NZ arrays with objects

    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [self.tableView setDelegate:self];
    [self.tableView setDatasource:self];
}
于 2012-10-10T11:33:15.753 に答える
1

あなたはこのようにしようとします、

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = @"Region";

    tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0,0,320,480) style:UITableViewStyleGrouped] autorelease];
    tableView.delegate=self;
    tableView.dataSource=self;
    [self.view addSubview:tableView];
    AM  = [[NSMutableArray alloc] init];
    NZ  = [[NSMutableArray alloc] init];

    [AM    addObject: @"Bologna"];
    [AM    addObject: @"Florence"];
    [AM    addObject: @"Milan"];
    [NZ    addObject: @"Naples"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    switch (section) {
        case 0:
            return [AM count];
            break;

        case 1:
            return [NZ count];
            break;

        default:
            return section;
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...

    NSInteger section = [indexPath section];

    switch (section) {
        case 0:
            [cell.textLabel setText: [AM objectAtIndex: [indexPath row]]];
            break;

        case 1:
            [cell.textLabel setText: [NZ objectAtIndex: [indexPath row]]];
            break;

        default:
            break;
    }    
    return cell;
}


- (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
{
  //  NSString * AMPath    = self.luzonRegion      [indexPath.row];
  //  NSString * NZPath    = self.visayasRegion    [indexPath.row];

    switch (indexPath.section) {
        case 0:
            //cityController.title = luzonRegionPath;
            NSLog(@"Selected city: %@", [AM objectAtIndex: [indexPath row]]);
            break;

        case 1:
            //cityController.title = visayasRegionPath;
            NSLog(@"Selected city: %@",[NZ objectAtIndex: [indexPath row]]);
            break;

        default:
            break;
    }

   // [[self navigationController] pushViewController:cityController animated:YES];
}

正常に動作しています.....

于 2012-10-10T11:44:27.607 に答える