【发布时间】:2016-04-08 06:36:23
【问题描述】:
我有一个填充表格视图的数组 - myPosts。
表格视图的第一行不是数组的一部分。
每一行都有自己的部分(有自己的自定义页脚)
我正在尝试使用以下代码执行删除:
func tableView(profileTableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
myPosts?.removeAtIndex(indexPath.section - 1)
profileTableView.beginUpdates()
let indexSet = NSMutableIndexSet()
indexSet.addIndex(indexPath.section - 1)
profileTableView.deleteSections(indexSet, withRowAnimation: UITableViewRowAnimation.Automatic)
profileTableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
profileTableView.endUpdates()
...
WS Call
...
}
}
日志报告如下:
无效更新:第 0 节中的行数无效。 包含在 更新后的现有部分 (1) 必须等于该部分中包含的行数 更新前的节(1),加上或减去插入或删除的行数 该部分(0 插入,1 删除)加上或减去移入或移出的行数 该部分(0 移入,0 移出)。'
显然问题与 0 移入,0 移出有关,但我不明白为什么会这样?或者解决方案是什么?
tableView的section数量如下:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if self.myPosts == nil
{
return 1
}
return self.myPosts!.count + 1
}
【问题讨论】:
-
请记住,在您删除索引 0 处的部分后,索引 1 处的部分将成为索引 0 处的新部分;因此,您还需要注意在您的 data-source 中反映这一点(我们对此一无所知),并且您还需要对行做一些事情。
-
@holex 我的数据源已在 Webservice 调用中更新,该调用已被注释掉。除了已经存在的 deleteRowsAtIndexPath 之外,我还需要对这些行做什么?
-
您在调用
–endUpdates方法之前必须完成更新本地数据源;否则你就完蛋了。 -
@holex 和行:myPosts?.removeAtIndex(indexPath.section - 1) 不处理更新数据源?!
-
我认为问题是在
deleteSections之后调用deleteRowsAtIndexPaths调用deleteSections不够?
标签: ios arrays swift uitableview nsindexpath