1

我有两个部分,AM 和 NZ。

我注意到,如果我在每个部分中有相同数量的城市,就不会有问题。但是,如果我没有相同数量的城市,程序将会崩溃。

错误是

-[__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

如果您UITableView直接在 XIB 中添加,有两种方法可以使用数组中的数据填充它。当您需要时,您应该将其直接绑定在程序中或delegate程序中datasourceIB

万一你不确定什么时候会显示数据,你可以给出它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 回答