【问题标题】:iOS array count returning erroriOS数组计数返回错误
【发布时间】:2012-10-01 17:33:25
【问题描述】:

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

【问题讨论】:

  • 在调试器中运行您的应用程序。您将看到一个堆栈跟踪,准确显示是哪一行代码导致了问题。您发布的代码不是导致崩溃的原因。问题将是某行试图访问数组中的元素。
  • 但是如何在- tableView: cellForRowAtIndexPath: 方法中访问单元格的值呢?这是一个盲目的猜测,但我认为您正在访问相同的 xmlParser.calls 数组以访问所有单元格.. xmlParser.calls * 2
  • 提供你的 - tableView: cellForRowAtIndexPath: 代码
  • 我发布了附加代码。
  • 我应该以不同的方式访问数组吗?

标签: ios arrays xml-parsing count tableview


【解决方案1】:

编辑:

仔细看看。只需添加/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];

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

【讨论】:

  • 你可能做错了什么,添加 /2 应该不会导致这种情况,除非你我们改变了其他东西
  • 太棒了。完美运行。 *我犯了一个愚蠢的错误并修复了它:P
【解决方案2】:

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

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

【讨论】:

  • 我收到此错误:由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'*** -[__NSCFConstantString stringByAppendingString:]: nil argument'
  • 如果我将它们注释掉,我仍然会收到相同的错误:由于未捕获的异常'NSRangeException'而终止应用程序,原因:'*** -[__NSArrayM objectAtIndex:]:索引 2 超出范围[0 .. 1]'
  • @JonErickson,第一个错误是由于您试图将nil(空白)字符串附加到NSString,您应该像if (str.lenght==0) {str = @" "; } 和第二个错误一样,我已经解释过了回答。
【解决方案3】:

你是在增加数组的数量,没什么大不了的

return [xmlParser.calls count] * 2;

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

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

【讨论】:

  • 这行不通(你打错字了,indexPath.row/2 但不是indexPath.row]/2
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-06
  • 1970-01-01
  • 1970-01-01
  • 2021-07-08
相关资源
最近更新 更多