Ios 删除第二节

Ios 删除第二节,ios,arrays,swift,uitableview,nsindexpath,Ios,Arrays,Swift,Uitableview,Nsindexpath,我有一个填充表视图的数组-myPosts 表视图的第一行不是数组的一部分 每行都是自己的节(具有自己的自定义页脚) 我正在尝试使用以下代码执行删除: func tableView(profileTableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if (editing

我有一个填充表视图的数组-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中的节数如下所示:

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        if self.myPosts == nil
        {

            return 1
        }
        return self.myPosts!.count + 1
    }

所以答案就是删除删除行的行

因此,代码在此删除:

 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 
            ...
        }
   }

更新了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
    }
}
没有必要使用和
endUpdates()
,因为您只执行一个包含动画的操作。如果您正在做2个或更多,那么将它们组合起来以获得流体效果是值得的


此外,这还利用了Swift 3类,取消了
NSMutableIndexSet()
调用,这需要现在进行转换才能使用
deleteSections()
调用。

请记住,删除索引0处的节后,索引1处的节将成为索引0处的新节;因此,您需要意识到在数据源中也反映了这一点(我们对此一无所知),并且您还需要对行进行处理。@holex my data source在已注释的Webservice调用中更新。除了已经存在的deleteRowsAtIndexPath之外,我还需要对这些行做什么?在调用
–endUpdates
方法之前,必须完成本地数据源的更新;否则您就注定要失败。@holex和行:myPosts?.removeAtIndex(indexPath.section-1)不能处理数据源的更新?!我认为问题在于在
deleteeSections
之后调用
deleteRowsAtIndexPaths
调用
deleteeSections
不够吗?