简体   繁体   English

将项目移至部分

[英]Move item to section

I can not understand what the problem is.我不明白是什么问题。 By clicking on the cell, the item is marked as completed and moves to the appropriate section.通过单击单元格,该项目被标记为已完成并移动到适当的部分。 But I noticed a bug, if after clicking on the item, click on it again, then it bugs (see gif below).但是我注意到一个错误,如果在单击该项目后再次单击它,则会出现错误(请参见下面的 gif)。 First, I change the state of the item, then delete it from the common array, and then add it to the corresponding section.首先,我改变项目的状态,然后从公共数组中删除它,然后将其添加到相应的部分。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let isCompleted: Bool
        tableView.beginUpdates()
        if isFiltering {
            let task = manager.filteredTasks[indexPath.row]
            task.isCompleted = !task.isCompleted
            isCompleted = task.isCompleted
        } else {
            isCompleted = manager.changeState(at: indexPath.section == 0 ? manager.toDoTasks[indexPath.row].id : manager.completedTasks[indexPath.row].id)
        }
        let lastTask: Task = manager.tasks[indexPath.row]
        manager.removeItem(with: lastTask.id)
        tableView.deleteRows(at: [indexPath], with: .automatic)
        if indexPath.section == 0 {
            manager.addTask(task: lastTask)
            tableView.insertRows(at: [IndexPath(row: manager.completedTasks.count - 1, section: 1)], with: .automatic)
        } else {
            manager.addTask(task: lastTask)
            tableView.insertRows(at: [IndexPath(row: manager.toDoTasks.count - 1, section: 0)], with: .automatic)
        }
        let accessoryType: UITableViewCell.AccessoryType = isCompleted ? .checkmark : .none
        tableView.cellForRow(at: indexPath)?.accessoryType = accessoryType
        tableView.endUpdates()
    }

 func changeState(at id: String) -> Bool {
        guard let task = tasks.first(where: { (item) -> Bool in
            item.id == id
        }) else { return false }
        task.isCompleted = !task.isCompleted
        computeSections()
        return task.isCompleted
    }

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: Keys.cell.rawValue, for: indexPath) as! ToDoCell
        print(indexPath)
        if isFiltering {
            cell.configureCell(task: manager.filteredTasks[indexPath.row])
        } else {
            cell.configureCell(task: indexPath.section == 0 ? manager.toDoTasks[indexPath.row] : manager.completedTasks[indexPath.row])
        }
        return cell
    }

漏洞

As already observed in the comments正如评论中已经观察到的

let lastTask: Task = manager.tasks[indexPath.row]

is suspicious and probably should be replaced with:是可疑的,可能应该替换为:

let lastTask = indexPath.section == 0
    ? manager.toDoTasks[indexPath.row]
    : manager.completedTasks[indexPath.row]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM