RealmSwift: MoveRow At IndexPath не работает - PullRequest
0 голосов
/ 01 мая 2020

У меня есть список AccountType, который содержит модели учетных записей, и мой TableView перечисляет их. как показано ниже.

Однако я не смог переместить RowAtIndexPath с использованием области, и даже если я переместил строку в indexPath, данные indexPath не изменились вообще.

Проблема 1: Если я переместлю (AccountType A account 1) в положение (AccountType A account 2). account 1 теперь будет отображать данные account 2

Проблема 2: tableView вернется к исходному списку, если я снова открою его, даже если я сохранил с помощью realm.write в moveRow At IndexPath.

Выпуск 3: Account(indexPath.row) должен быть в пределах AccountType(indexPath.Section) Я могу переместить его в любой раздел.

 var listAccountType  = List<AccountType>()
    var listAccount = List<Account>()
AccountType A //indexPath.section
- account 1 //indexPath.row
- account 2 //indexPath.row
- account 3 //indexPath.row

AccountType B //indexPath.section
- account 1
- account 2
- account 3

AccountType C //indexPath.section
- account 1
- account 2
- account 3

override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
         override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        try! realm.write {
            let sourceObject = listAccountType[sourceIndexPath.section].ofAccount[sourceIndexPath.row]
              let destinationObject = listAccountType[sourceIndexPath.section].ofAccount[sourceIndexPath.row]

            let destinationObjectOrder = destinationObject

              if sourceIndexPath.row < destinationIndexPath.row {
              for index in sourceIndexPath.row...destinationIndexPath.row {
                let object = listAccount[index]
                object -= 1
              }
              } else {
              for index in (destinationIndexPath.row..<sourceIndexPath.row).reversed() {
              let object = listAccount[index]
                object += 1
              }
              }
              sourceObject.direction = destinationObjectOrder
              }

             ///Require int variable in  Account() for indexing : create @objc dynamic var accIndex : Int = 0 first
             try! listAccount.realm?.write {
                 listAccount.move(from: sourceIndexPath.row, to: destinationIndexPath.row)
                 self.tableView.moveRow(at: sourceIndexPath, to: destinationIndexPath)
             }
             print("moved Account")
       }


    override func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        return true
    }
...