0

配列をカウントしてテーブルの行数として設定し、各セルの間に非表示のセルがあり、セルが分離されているように見えるため、2を掛けようとしています。ビルドしようとするたびに、次のエラーが発生します。

キャッチされなかった例外'NSRangeException'が原因でアプリを終了しています。理由:' * -[__ NSArrayM objectAtIndex:]:インデックス3が境界を超えています[0 .. 2] '

これは私が使用しているコードです:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return [xmlParser.calls count] * 2; 
}

cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
  static NSString *CellIdentifier = @"Cell";
  static NSString *CELL_ID2 = @"SOME_STUPID_ID2";
  JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];
  self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"texture3.png"]];

  [self.tableView setSeparatorColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"texture3.png"]]];

  if (indexPath.row % 2 == 1) {
    UITableViewCell * cell2 = [tableView dequeueReusableCellWithIdentifier:CELL_ID2];

    if (cell2 == nil) {
      cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:CELL_ID2];
      [cell2.contentView setAlpha:0];
      [cell2 setUserInteractionEnabled:NO];
      [cell2 setBackgroundColor:[UIColor clearColor]];
    }
    return cell2;
  }

  TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  if (cell == nil) {
    cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  }

  cell.callTypeLabel.text = currentCall.currentCallType;
  cell.locationLabel.text = currentCall.location;
  cell.unitsLabel.text = currentCall.units;
  cell.stationLabel.text = [@"Station: " stringByAppendingString:currentCall.station];
  cell.contentView.layer.backgroundColor = [UIColor whiteColor].CGColor;
  cell.contentView.layer.frame = CGRectMake(0, 0, cell.contentView.frame.size.width, cell.contentView.frame.size.height);
  cell.contentView.layer.borderWidth = 1.0;
  cell.contentView.layer.borderColor = [UIColor blackColor].CGColor;

  if ([currentCall.county isEqualToString:@"W"]) {  
    cell.countyImageView.image = [UIImage imageNamed:@"blue.png"];
  } else {
    cell.countyImageView.image = [UIImage imageNamed:@"green.png"];
  }

  if ([currentCall.callType isEqualToString:@"F"]) {  
    cell.callTypeImageView.image = [UIImage imageNamed:@"red.png"];
  } else {
    cell.callTypeImageView.image = [UIImage imageNamed:@"yellow.png"];
  }
  return cell;
}
4

3 に答える 3

0

編集:

よく見てみました。を追加するだけ/2です:

JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row / 2];

そして、それはうまくいくでしょう。以下の からの同じ回答に気付きましたrichard


マユールが気づいたように:

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

// n = [xmlParser.calls count]
// Ok, we have n*2 items (but calls has only n!):
return [xmlParser.calls count] * 2;
// calls has n items in it, but you return n*2 items

そして、で- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

// n = [xmlParser.calls count]
// give me object from calls at index (which will be in range [0; n*2 - 1], 
// which could be beyond [0; n-1] which you originally have)
JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];

xmlParser.calls が空の配列でない限り、常にこのエラーが発生します。

于 2012-10-11T06:07:43.270 に答える
0

cell.textLabel.text = [array objectAtIndex:indexPath.row];制限を超えたようなことをしているときにエラーが発生しました。ただし、その間に非表示の(空白の)セルを残す必要があるため、データソースメソッドでいくつかのif...else条件を設定して、配列の制限を防ぐことができます。の適切なカウントも設定します。UITableViewcellForRowAtIndexPathreturnnumberOfCells

配列に4つの値がある場合のようなものです。したがって、3 つの目に見えないセルを作成する必要があります。

1st Cell

blank

2nd Cell

blank

3rd Cell

blank

4th Cell

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    return [xmlParser.calls count] + (([xmlParser.calls count]%2)+1);
}

あなたのcellForRowAtIndexPath方法では、次のような条件を実行します

if(indexPath.row==0) 
{ 
    cell.textLabel.text = [array objectAtIndex:indexPath.row]; 
} 
else if((indexPath.row+1)%2!=0) 
{ 
   cell.textLabel.text = [array objectAtIndex:indexPath.row]; 
}
else
{
   NSLog(@"%d is blank cell",indexPath.row);
}
于 2012-10-11T06:19:31.180 に答える
0

配列の数を増やしていますが、大したことではありません

return [xmlParser.calls count] * 2;

indexPath.rowただし、データソースの実際のインデックスを取得するには、を で割って 2 にする必要があります。この表は、サイズが 2n のデータソースがあることを前提としているため、テーブルはインデックスパスを使用して 0 .. 2n にアクセスします。

JointCAD *currentCall = [[xmlParser calls] objectAtIndex:(indexPath.row/2)]
于 2012-10-11T06:24:09.103 に答える