0

我有以下代码

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

}
4

2 回答 2

0

The issue is that you aren't changing the size of _imgList (from which, I assume, is derived the return value of the tableView:numberOfRowsInSection: method, but you don't show this method). By inserting a row in the table view, but not updating the data source to reflect that new row, you have created an inconsistency in your application.

To ensure that this doesn't happen, make the changes to your data source that reflect exactly the changes you are making to the rows, and do so before inserting or deleting any rows.

You don't show code that declares or instantiates _imgList so if it's not an NSMutableArray you'll need to make it one, and use methods like addObject:, insertObject:atIndex:, and removeObjectAtIndex: to insert and remove the appropriate objects. In your case, your code would become something like this:

NSInteger indexToAdd = _imgList.count;
[_imgList addObject:@"whatever"];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:indexToAdd 
                                            inSection:0];
[self.resultList insertRowsAtIndexPaths:@[indexPath]
                       withRowAnimation:UITableViewRowAnimationFade];
于 2013-01-22T06:53:23.953 回答
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;

它可能会解决你的问题:)

于 2013-01-21T11:57:53.640 回答