0

我有一个 UITableView ,其中包含我试图从中删除行的部分。在从 BubbleWrap 切换到 AFMotion 时,我开始遇到一些不可变数组错误。通常,它们可以通过在推动其视图控制器之前在我正在更改的对象上附加一个 mutableCopy 来修复。但是,对于带有部分的表,我克服了冻结/不可变数组错误,但得到了一个NSInternalInconsistencyException: Invalid update: invalid number of rows in section 0. 我已经看到有关这些的其他问题并尝试添加beginUpdates,endUpdates等,但它仍然无法正常工作。

我的数据对象是一个看起来像这样的哈希:

{
  "Items" => ["one", "two", "three"],
  "More"  => ["four", "five", "six"]
}

当我展示它时,我将该数据传递给一个新的控制器:

def tap_items(sender)
  controller        = ItemsController.alloc.initWithNibName(nil, bundle:nil)
  controller.data   = @data[:items].mutableCopy
  self.presentViewController(
    UINavigationController.alloc.initWithRootViewController(controller),
    animated:true,
    completion: lambda {}
  )
end

然后在ItemsController我的表中设置删除具有以下内容的行:

def tableView(tableView, commitEditingStyle:editingStyle, forRowAtIndexPath:indexPath)
  if editingStyle == UITableViewCellEditingStyleDelete
    rows_for_section(indexPath.section).delete_at indexPath.row
    tableView.deleteRowsAtIndexPaths([indexPath],
      withRowAnimation:UITableViewRowAnimationFade)
  end
end

但这会引发内部不一致异常错误。我也试过data[sections[indexPath.section]].delete_at indexPath.row代替rows_for_section(indexPath.section).delete_at indexPath.row. 但这不会改变任何事情。

更新:这是我的 tableView 的代码

def viewDidLoad
  super

  @table = UITableView.alloc.initWithFrame(self.view.bounds)
  @table.autoresizingMask = UIViewAutoresizingFlexibleHeight
  self.view.addSubview(@table)

  @table.dataSource = self
  @table.delegate = self
end

def sections
  data.keys.sort
end

def rows_for_section(section_index)
  data[self.sections[section_index]].mutableCopy
end

def row_for_index_path(index_path)
  rows_for_section(index_path.section)[index_path.row]
end

def tableView(tableView, titleForHeaderInSection:section)
  sections[section]
end

def numberOfSectionsInTableView(tableView)
  self.sections.count
end

def tableView(tableView, numberOfRowsInSection: section)
  rows_for_section(section).count
end
4

1 回答 1

0

It sounds like somehow you are not referencing the correct instance of your dataSource. You don't show where you are assigning it, i.e. tableView.dataSource = data, nor the code for rows_for_section. I'm wondering whether it has to do with the .mutableCopy. Is that necessary for some reason?

In my app, I pull in the dataSource directly from the tableView, i.e. tableView.dataSource.

于 2013-06-27T20:03:38.777 回答