我有一个 AccountType 列表,其中包含 Account 模型,而我的 TableView 正在列出它们。如下所示。
但是,我无法使用领域移动RowAtIndexPath,即使我确实移动了 indexPath 中的行,indexPath 数据也根本不会改变。
问题 1:如果我移动(AccountType A account 1)
到(AccountType A account 2)
位置。account 1
现在将显示的数据account 2
问题 2:即使我在 moveRow At IndexPath 中使用 realm.write 保存了 tableView,如果我重新打开它,它也会返回到原始列表。
问题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
}