我正在尝试从UITableView删除行

-(void) closePickerViewRow:(id) sender {

    if(self.pickerIsShown && [self.forecastsData count] > 0){

      [self.tableView beginUpdates];
      NSMutableArray* tempArray = [self.forecastsData mutableCopy];
      NSIndexPath* indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
      [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
      [tempArray removeObjectAtIndex:1];
      self.forecastsData = [tempArray copy];
      [self.tableView 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 (11)
must be equal to the number of rows contained in that section before the
update(11),plus or minus the number of rows inserted or deleted from that
section (0 inserted, 1 deleted) and plus or minus the number of rows moved into
or out of that section (0 moved in, 0 moved out).'

我更新了DataSource self.forecastData,在删除行(开始时为11)后,它具有10个元素。因此,元素的数量是正确的。问题出在哪里?

最佳答案

在告诉tableview之前删除数据:

[tempArray removeObjectAtIndex:1];
self.forecastsData = [tempArray copy];
[self.tableView deleteRowsAtIndexPaths:@[indexPath]
                      withRowAnimation:UITableViewRowAnimationFade];

关于ios - NSInternalInconsistencyException异常uitableview,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26782946/

10-15 04:39