ios - 重新排序 UITableView 中的部分

标签 ios uitableview move sections

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

最佳答案

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

手势应该比较明显。在标题上开始后,您需要在表格 View 中使用 locationIn 来跟踪位置。

要折叠行,您需要做的就是使用适当的动画修改您的表格 View 单元格,例如:

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()

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

此时您可能需要创建一个额外的 View 来表示您的部分堆栈并跟随您的手指。如果您将其添加为 subview 并使用其 super View 中的平移手势识别器 locationIn 操作它的中心,这应该非常容易:

movableSectionView.center = panGestureRecognizer.location(in: movableSectionView.superview!) 

到目前为止,您应该能够抓取折叠所有单元格的部分,并能够拖动“部分堆栈” View 。现在您需要检查您的手指在 TableView 中的位置,以了解将部分拖放到何处。这有点痛苦,但可以通过 visibleCellstableView.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)
        }
    }
}

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

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

这基本上应该足以重新排序,但您可能希望在表格 View 中显示拖动部分所在的位置。就像在堆栈将被放入的部分的末尾有一个占位符。这应该相对容易,只需添加然后 move 一个额外的占位符单元,重用 indexPathForGestureRecognizer 来获取它的位置。

玩得开心。

关于ios - 重新排序 UITableView 中的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52607403/

相关文章:

swift - awakeFromNimb 不会在 TableView reloadData Swift 上被调用

ios - reloadItemsAtIndexPaths 上的 Collection View 崩溃

ios - 如何使用 UIPanGestureRecognizer move 2 个或更多 UIImageViews

java - 如何将一个 JLabel 的内容传输到另一个 JLabel?

ios - 如何将嵌入式 youtube 视频旋转为横向模式

iOS-MPMoviePlayerController 无法从远程 URL 播放视频

ios - 删除 iOS 中谷歌地图聚类中的特定标记

ios - 通过 iPhone 设备扫描三个具有相同 CBUUID 但 MAC 地址不同的相同 BLE 传感器

ios - 带有 CustomCell 的 UITableView 在 Xcode9 上不显示数据 Swift4

visual-studio - Resharper 5.1 Refactor (Refactor->Move) 是否正确通知 AnkhSVN/维护 SVN 历史?