【Swift】UITableViewのセクション間の並び替え制限

UITableViewのセクション間の並び替えを制限する

UITableViewのDelegateのtableView(_:targetIndexPathForMoveFromRowAt:toProposedIndexPath:)を使用することで制限が行えます

これによりセクションを跨いだセルの移動を制限します

同一セクション内の移動は正常に動作します。

func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {    
    if sourceIndexPath.section == proposedDestinationIndexPath.section {
        return proposedDestinationIndexPath
    }
    return sourceIndexPath
}

参考URL

https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614953-tableview