RealmSwift: ошибка MoveRow в IndexPath
У меня есть список 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
}