gpt4 book ai didi

ios - 单击单元格时如何根据排序移动单元格?

转载 作者:行者123 更新时间:2023-11-29 05:41:35 26 4
gpt4 key购买 nike

我正在寻找一种方法,当根据排序点击单元格到其位置时移动单元格。

我在 Google 上搜索过,但没有找到太多东西,而且我个人以前从未做过这样的事情。

这是我的排序

 let sort = NSSortDescriptor(key: #keyPath(Item.name), ascending: true)

这是我的didSelectRow

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item = fetchedRC.object(at:indexPath)

item.isComplete.toggle()

do {
try context.save()
//tableView.deselectRow(at: [indexPath.row], animated: true)
//tableView.reloadRows(at: [indexPath], with: .automatic)
} catch let error as NSError {
print("Could not save. \(error.localizedDescription)")
}
}

我希望将选定的单元格划掉(我已经处理过)并移动到 tableView 的底部,这就是我的排序正在做的事情。正如预期的那样,排序仅在加载时有效,但我想在表格初始出现后将选中的单元格移动到其各自的位置。

编辑: 我决定测试排序,并将其设置为我的名字而不是我的 isComplete 字段,因此它现在相应地移动它们,但现在它不会像应该的那样划掉文本。

最佳答案

我终于找到了这个解决方案,它似乎按预期工作。我所需要做的就是将令人兴奋的 .move 替换为此。

case .move:
if indexPath != nil {
self.tableView.deleteRows(at: [currentIndexPath!], with: .automatic)
}
if let newIndexPath = newIndexPath {
self.tableView.insertRows(at: [newIndexPath], with: .automatic)
}

关于ios - 单击单元格时如何根据排序移动单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56497846/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com