Ios 在UITableView中重新排列节

Ios 在UITableView中重新排列节,ios,uitableview,move,sections,Ios,Uitableview,Move,Sections,我有带节和行的UITableView()。我知道如何为行启用重新排序,但我不知道如何为节实现重新排序。 我需要将重新排序控件添加到sections()中,然后当用户触摸此控件时,section下的行应该会失败。之后,用户可以将节移动到新位置。 在阅读了stackoverflow和其他网站上的主题之后,我没有发现任何关于如何实现这样的东西的好主意。 我曾考虑过通过单元格实现节,但在这种情况下,我不能为了进一步移动到新位置而将节下的行折叠起来。 如果你知道如何实现这一点,请给我一些建议。谢谢 没有本

我有带节和行的UITableView()。我知道如何为行启用重新排序,但我不知道如何为节实现重新排序。 我需要将重新排序控件添加到sections()中,然后当用户触摸此控件时,section下的行应该会失败。之后,用户可以将节移动到新位置。 在阅读了stackoverflow和其他网站上的主题之后,我没有发现任何关于如何实现这样的东西的好主意。 我曾考虑过通过单元格实现节,但在这种情况下,我不能为了进一步移动到新位置而将节下的行折叠起来。
如果你知道如何实现这一点,请给我一些建议。谢谢

没有本机功能来实现您想要的功能。如果我理解正确,您可能希望折叠一整段行,然后开始拖动“标题”。如果你想自己做这件事,我建议从一个平移手势识别器开始,它会在标题按钮上触发

这个姿态应该相对明显。在标题上启动后,您需要在表视图中使用
locationIn
跟踪位置

要折叠行,只需使用适当的动画修改表视图单元格,如:

tableView.beginUpdates()
tableView.deleteSections([myIndexPath], with: .top) // Maybe experiment with animation type
// Modify whatever you need to correspond this change in the data source
tableView.endUpdates()
由于您将删除该部分,因此也将删除带有手势识别器的视图(标题)。这意味着最好直接将手势添加到表视图或其superview。您只需要在按下标题上的其中一个按钮时强制触发它。你可以对此有所了解。其余部分不受此变化的影响

此时,您可能需要创建一个额外的视图,该视图表示剖面堆栈并跟随您的手指。如果您将其添加为子视图,并在其superview中使用平移手势识别器
locationIn
操纵其中心,这应该非常容易:

movableSectionView.center = panGestureRecognizer.location(in: movableSectionView.superview!) 
因此,到目前为止,您应该能够获取一个折叠所有单元格的截面,并能够拖动“截面堆栈”视图。现在,您需要检查您的手指在表视图中的位置,以知道将节放置在何处。这有点痛苦,但可以通过
visibleCells
tableView.indepath(for:)

这基本上足以进行重新排序,但您可能希望在表视图中显示被拖动的部分的位置。就像在要将堆栈放入的部分的末尾有一个占位符一样。通过简单地添加并移动一个额外的占位符单元格,然后重用InExpathForgestureRecognizer来获得它的位置,这应该是相对容易的


玩得开心

哇,这么详细和有用的答案。谢谢你的帮助。我会努力去理解和实现它。
func indexPathForGestureRecognizer(_ recognizer: UIGestureRecognizer) -> IndexPath {
    let coordinateView: UIView = tableView.superview! // This can actually be pretty much anything as long as it is in hierarchy
    let y = recognizer.location(in: coordinateView).y
    if let hitCell = tableView.visibleCells.first(where: { cell in
        let frameInCoordinateView = cell.convert(cell.bounds, to: coordinateView)
        return frameInCoordinateView.minY >= y && frameInCoordinateView.maxY <= y
    }) {
        // We have the cell at which the finger is. Retrieve the index path
        return tableView.indexPath(for: hitCell) ?? IndexPath(row: 0, section: 0) // This should always succeed but just in case
    } else {
        // We may be out of bounds. That may be either too high which means above the table view otherwise too low
        if recognizer.location(in: tableView).y < 0.0 {
            return IndexPath(row: 0, section: 0)
        } else {
            guard tableView.numberOfSections > 0 else {
                return IndexPath(row: 0, section: 0) // Nothing in the table view at all
            }
            let section = tableView.numberOfSections-1
            return IndexPath(row: tableView.numberOfRows(inSection: section), section: section)
        }
    }
}
tableView.beginUpdates()
// Modify whatever you need to correspond this change in the data source
tableView.insertSections([indexPathForGestureRecognizer(panGestureRecognizer).section], with: .bottom)
tableView.endUpdates()