对 UITableView 中的部分重新排序

Ban*_*iuk 1 move uitableview ios sections

我有 UITableView 与部分和行(https://i.stack.imgur.com/p1Xuq.jpg)。我知道如何启用行重新排序,但我不知道如何实现部分重新排序。\n我需要向部分添加重新排序控件(https://i.stack.imgur.com/GgLV7.jpg),然后当用户触摸此控件时,部分下的行应该失败。之后用户可以将部分移动到新位置。\n在阅读 stackoverflow 和其他网站上的主题后,我没有找到任何好主意如何实现这样的东西。\n我考虑过通过单元格实现部分,但在这种情况下我不能\n翻动部分下的行以进一步移动到新位置。\n如果您知道如何实现此 \xe2\x80\x93,请给我建议。谢谢!

\n

Mat*_*lak 5

没有原生功能可以实现您想要的功能。如果我理解正确的话,您会想要折叠整个行部分,然后开始拖动“标题”。如果您想自己执行此操作,我建议您从在标题按钮上触发的平移手势识别器开始。

手势应该是比较明显的。在标题上开始后,您需要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()
Run Code Online (Sandbox Code Playgroud)

由于您将删除该部分,因此您还将删除具有手势识别器的视图(标题)。这意味着最好将手势直接添加到表视图或其超级视图中。您需要强制它仅在按下标题上的这些按钮之一时触发。你可以在这里得到一些关于它的想法。其余部分不受此更改的影响。

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

movableSectionView.center = panGestureRecognizer.location(in: movableSectionView.superview!) 
Run Code Online (Sandbox Code Playgroud)

因此,到目前为止,您应该能够抓住一个折叠所有单元格的部分,并能够拖动“部分堆栈”视图。现在您需要检查手指在表格视图中的位置,以了解将该部分放在哪里。这有点痛苦,但可以用visibleCellsand来完成tableView.indexPath(for: )

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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

手势识别器结束后,您可以使用此方法来获取您要放入项目的部分。所以就:

tableView.beginUpdates()
// Modify whatever you need to correspond this change in the data source
tableView.insertSections([indexPathForGestureRecognizer(panGestureRecognizer).section], with: .bottom)
tableView.endUpdates()
Run Code Online (Sandbox Code Playgroud)

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

玩得开心。