NSInternalInconsistencyException添加比预期更多的元素

时间:2013-01-21 11:41:17

标签: iphone xcode uitableview

我有以下代码

[self.tV beginUpdates];
NSIndexPath *iP = [NSIndexPath indexPathForRow:indexArray.count inSection:0];
[indexArray addObject:iP];
[self.tV insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];
[self.tV endUpdates];

我收到以下错误

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason:
'Invalid update: invalid number of rows in section 0.  The number of rows contained in an 
existing section after the update (2) must be equal to the number of rows contained in 
that section before the update (1), plus or minus the number of rows inserted or deleted
from that section (2 inserted, 0 deleted) and plus or minus the number of rows moved into
or out of that section (0 moved in, 0 moved out).'

我不确定2插入的来自哪里。每次单击按钮时都会调用此代码。第一次在indexArray中有一个元素,如代码中所示,我添加了一个元素,但似乎它试图再次添加两个元素。这是对的吗?

更新

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

  if(_imgList.count>0)
  {
      NSMutableString *fileName = [[NSMutableString alloc]
                                    initWithString:[NSString stringWithFormat: @"img"]];
      [fileName appendString: [_imgList objectAtIndex: _resultTag]];
      UIImage *image = [UIImage imageNamed: fileName];
      UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
      imageView.frame = CGRectMake(_xPos, 0, image.size.width, image.size.height);
      [cell addSubview: imageView];

      _xPos += SPACING;
  }

  return cell;
}


- (IBAction)btn:(UIButton *)sender {
    [self.resultList beginUpdates];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:indexArray.count inSection:0];
    [indexArray addObject:indexPath];
    [self.resultList insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];
[self.resultList endUpdates];

}

2 个答案:

答案 0 :(得分:0)

<强>·H

int insertRow;

<强>的.m

- (void)viewDidLoad
{
     self.insertRow = self.YourArr.count;
}

[self.tV beginUpdates];
    NSIndexPath *iP = [NSIndexPath indexPathForRow:self.insertRow inSection:0];
    [indexArray addObject:iP];
    [self.tV insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];
    [self.tV endUpdates];

self.insertRow = self.insertRow + 1;

可能会解决您的问题:)

答案 1 :(得分:0)

问题是你没有改变_imgList的大小(我假设,从中导出了tableView:numberOfRowsInSection:方法的返回值,但你没有显示这个方法) 。通过在表视图中插入一行,但不更新数据源以反映该新行,您在应用程序中创建了一个不一致的地方。

要确保不会发生这种情况,请对数据源进行更改,以准确反映您对行所做的更改,并在插入或删除任何行之前执行此更改。

您不会显示声明或实例化_imgList的代码,因此如果它不是NSMutableArray,您需要将其设为一个,并使用addObject:,{{1}等方法}和insertObject:atIndex:插入和删除适当的对象。在您的情况下,您的代码将变为这样:

removeObjectAtIndex: