【问题标题】:How do I move cells according to a sort when a cell is clicked?单击单元格时如何根据排序移动单元格?
【发布时间】:2019-10-23 04:03:42
【问题描述】:

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

我在 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 字段,所以它现在相应地移动它们,但现在它不会像它应该做的那样划掉文本。

【问题讨论】:

  • 如果表格视图由获取的结果控制器驱动,则 UI 应在保存上下文后自动更新。
  • 哪些部分需要更新?整个表格视图还是单元格?
  • 如果实现了NSFetchedResultsController 的委托方法,则这些方法会更新 UI。无需手动重新加载行。
  • 好的,所以删除 deselectRowreloadRows?
  • 在默认实现中,didChange 方法都包含对应于NSFetchedResultsChangeTypeinsert/delete/update/move 表视图方法

标签: ios swift uitableview core-data ios13


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-28
    • 2022-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-26
    • 1970-01-01
    相关资源
    最近更新 更多