当前位置:   iOS > 正文

删除indexPath swift部分

arrays,uitableview,nsindexpath,ios,swift,DevBox,在线流程图,编程,编程问答,程序员,开发者工具,开发工具,json解析,二维码生成,unix时间戳,在线开发工具,前端开发工具,开发人员工具,站长工具

我有一个填充表视图的数组 - 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
    }

CodeBender.. 8

更新了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
    }
}

不需要使用beginUpdates()endUpdates(),因为您只需要执行一个包含动画的动作。如果您执行2次或更多操作,则值得将它们组合以获得流畅的效果。

另外,通过取消NSMutableIndexSet()调用,这将利用Swift 3类,这需要立即进行转换才能使用该deleteSections()调用。



1> CodeBender..:

更新了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
    }
}

不需要使用beginUpdates()endUpdates(),因为您只需要执行一个包含动画的动作。如果您执行2次或更多操作,则值得将它们组合以获得流畅的效果。

另外,通过取消NSMutableIndexSet()调用,这将利用Swift 3类,这需要立即进行转换才能使用该deleteSections()调用。



2> Ismail..:

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

所以代码在这里删除:

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/iOS/detail/8867
推荐阅读
相关标签
  

闽ICP备14008679号