iOS数组计数返回错误

时间:2012-10-11 05:50:39

标签: ios arrays xml-parsing count tableview

我正在尝试将我的数组设置为我的表中的行数,然后将其乘以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;
}

3 个答案:

答案 0 :(得分:0)

修改

仔细看看。只需添加/2

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

它会起作用。 刚从richard注意到以下相同的答案。


正如mayuur注意到的那样:

- (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];

所以你应该把它乘以2或者你自己建议一些东西,除非xmlParser.calls是一个空数组,否则你总会得到这个错误。

答案 1 :(得分:0)

当您执行类似cell.textLabel.text = [array objectAtIndex:indexPath.row];之类的操作时,会发生错误。但是,您需要在其间留下不可见(空白)单元格,您可以在if...else数据源UITableView方法中设置一些cellForRowAtIndexPath条件,以防止数组限制。同时为return设置正确的numberOfCells计数。

类似的,如果您的数组中有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);
}

答案 2 :(得分:0)

你正在乘以数组计数,没什么大不了的

return [xmlParser.calls count] * 2;

但是您应该将indexPath.row除以2以获得数据源的真实索引。该表假定您有一个大小为2n的数据源,因此该表将使用索引路径访问0 ... 2n。

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