use*_*025 2 arrays uitableview nsindexpath ios swift
我有一个填充表视图的数组 - 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
...
}
}
Run Code Online (Sandbox Code Playgroud)
并且日志报告以下内容:
无效更新:第0节中的行数无效.更新(1)后现有部分中包含的行数必须等于更新前的该部分中包含的行数(1),加上或减去数字从该部分插入或删除的行(0已插入,1已删除)以及加或减移入或移出该部分的行数(0移入,0移出).
显然问题是0移入,0移出但我不明白为什么会这样?或解决方案是什么?
tableView中的节数如下:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if self.myPosts == nil
{
return 1
}
return self.myPosts!.count + 1
}
Run Code Online (Sandbox Code Playgroud)
更新了Swift 4.2的答案,并进行了其他一些调整:
func tableView(_ tableView: UITableView,
commit editingStyle: UITableViewCell.EditingStyle,
forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
myPosts?.removeAtIndex(indexPath.section - 1)
let indexSet = IndexSet(arrayLiteral: indexPath.section)
profileTableView.deleteSections(indexSet, with: .automatic)
// Perform any follow up actions here
}
}
Run Code Online (Sandbox Code Playgroud)
不需要使用beginUpdates()和endUpdates(),因为您只需要执行一个包含动画的动作。如果您执行2次或更多操作,则值得将它们组合以获得流畅的效果。
另外,通过取消NSMutableIndexSet()调用,这将利用Swift 3类,这需要立即进行转换才能使用该deleteSections()调用。
所以答案就是删除删除行的行.
所以代码在这里删除:
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
...
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11167 次 |
| 最近记录: |