gpt4 book ai didi

ios - 从 UITableView 中删除值会导致崩溃

转载 作者:行者123 更新时间:2023-11-30 11:53:46 26 4
gpt4 key购买 nike

我有一个 UITableView,当我单击一个单元格时,我将 firebase 中的值更新为 true。我还实现了删除 UITableViewCell 的功能。问题是,如果单元格已被单击(更新)然后删除,应用程序就会崩溃。当我查看 firebase 时,已更新的值尚未被删除(其他所有内容均已删除),只有在单元格已更新时才会发生这种情况。这就是我得到的

// remove task

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

let taskToDelete = groupTask[indexPath.row]

if editingStyle == UITableViewCellEditingStyle.delete {
groupTask.remove(at: indexPath.row)
DataService.instance.REF_GROUPS.child(group!.key).child("task").child(taskToDelete.id).removeValue(completionBlock: { (error, refer) in
if error != nil {

} else {

}
})
self.tableView.reloadData()
}
}

// update task value

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let cell = tableView.cellForRow(at: indexPath) as? GroupTaskCell else { return }

if cell.isSelected == true {
let selected = groupTask[indexPath.row]
DataService.instance.REF_GROUPS.observe(.value) { (snapShot) in
DataService.instance.updateTaskStatus(desiredGroup: self.group!, selected: true, atIndexpath: indexPath.row, childPath: selected.id, handler: { (complete) in
self.tableView.reloadData()
})
}
} else {

}
self.tableView.reloadData()
}


// update task status

func updateTaskStatus(desiredGroup: Group, selected: Bool, atIndexpath: Int, childPath: String, handler: @escaping (_ taskArray: [Task]) -> ()) {

REF_GROUPS.child(desiredGroup.key).child("task").child(childPath).updateChildValues(["selected": selected])
}

编辑!

Could not cast value of type 'NSNull' (0x1b5915f18) to 'NSString' (0x1b5921ad8). 2018-01-01 17:38:53.431901+0200 WireUp[587:103199] Could not cast value of type 'NSNull' (0x1b5915f18) to 'NSString' (0x1b5921ad8). (lldb)

删除任务之前 Firebase 的样子: What firebase looks like before I delete the task

之后:And after

正如您所看到的,它并没有删除已更新的值,这就是导致崩溃的原因。我感谢所有帮助。

func addTask(withTask task: String, andPeople people: String, forUID uid: String, withGroupKey groupKey: String?, selectedStatus selected: Bool, sendComplete: @escaping (_ taskCreated: Bool ) -> ()) {
if groupKey != nil {
REF_GROUPS.child(groupKey!).child("task").childByAutoId().updateChildValues(["taskToDo": task, "peopleToDoTask": people, "senderId": uid, "selected": selected])
sendComplete(true)
} else {

}
}

func updateTaskStatus(desiredGroup: Group, selected: Bool, childPath: String, handler: @escaping (_ taskArray: [Task]) -> ()) {

REF_GROUPS.child(desiredGroup.key).child("task").child(childPath).updateChildValues(["selected": selected])
}


func configureTaskCell(taskToDo task: String, nameForPerson name: String, isSelected: Bool ) {
self.task.text = task
self.name.text = name
if isSelected {
self.chechImg.isHidden = false
} else {
self.chechImg.isHidden = true
}
}

最佳答案

而不是使用 self.tableView.reloadData()从 TableView 委托(delegate)函数中尝试使用 beginUpdate() 更新和删除单元格和endUpdate() UITableView 的方法。你不会崩溃,也不会重新加载整个表。在 Obj-C 中,删除和更新单元格如下所示:

[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];

[tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects: indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];

关于ios - 从 UITableView 中删除值会导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48050843/

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