быстрое изменение порядка ячеек uitableview, кроме последней - PullRequest
0 голосов
/ 31 октября 2018

Несколько дней назад я получил отличный совет от Stack Overflow. Это должно было предотвратить перемещение последней ячейки внутри UITableView. Используется следующий код.

func tableView (_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        if indexPath.row <myTableviewArray.count {
            return true
        }
        return false
    }

И код, который я использовал для перемещения ячеек:

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        if destinationIndexPath.row != menuDic.count {
            let movedObject = menuDic[sourceIndexPath.row]
            menuDic.remove(at: sourceIndexPath.row)
            menuDic.insert(movedObject, at: destinationIndexPath.row)
        }
    }

Но возникла неожиданная проблема. Другая камера двигалась под последнюю камеру! Клетки, кроме последней, не должны двигаться ниже последней клетки. Последняя ячейка всегда должна быть последней.

Я использовал следующие два кода, но все не удалось.

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

func tableView (_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        if destinationIndexPath.row! = myTableviewArray.count {
            let movedObject = myTableviewArray [sourceIndexPath.row]
            myTableviewArray.remove (at: sourceIndexPath.row)
            menuDic.insert (movedObject, at: destinationIndexPath.row)
        }
    }

Используя эти два кода, ячейка все равно будет перемещаться под последнюю ячейку. Переполнение стека. Спасибо вам все время! :)

1 Ответ

0 голосов
/ 31 октября 2018

indexPath.row начинается с нуля. если myTableviewArray является вашим источником данных.

indexPath.row <myTableviewArray.count, это будет всегда верно.

попробуйте использовать:

func tableView (_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        if indexPath.row < myTableviewArray.count - 1 {
            return true
        }
        return false
    }
...