【问题标题】:delete section at indexPath swift快速删除 indexPath 处的部分
【发布时间】: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


【解决方案1】:

更新了 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 个或更多,那么值得将它们组合起来以获得流畅的效果。

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

【讨论】:

    【解决方案2】:

    所以答案只是删除删除行的行。

    所以这里的代码要删除:

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

    【讨论】:

      猜你喜欢
      • 2022-11-10
      • 2018-09-26
      • 1970-01-01
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-13
      相关资源
      最近更新 更多