gpt4 book ai didi

arrays - 想从tableview中删除一行,并从数组中删除对应的对象

转载 作者:行者123 更新时间:2023-11-28 13:35:58 25 4
gpt4 key购买 nike

我想从 Swift 5 中的 TableView 中删除一行并从数组中删除该对象。我搜索了很多,但无法完成。

我尝试了 StackOverflow 上可用的所有相关解决方案,但找不到。

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
let position = indexPath.row
if (editingStyle == .delete) {

labDetailsTableView.beginUpdates()
if let idx = labs.firstIndex(where: { $0 === position }) {
labs.remove(at: idx)
}


labDetailsTableView.endUpdates()
}
}

Binary operator '===' cannot be applied to operands of type 'LabDetails' and 'Int'

最佳答案

labs.remove(at: position) 替换 if let block 。不需要 firstIndex

您也不需要调用 beginUpdatesendUpdates。但是您确实需要添加对 tableView.deleteRows(at: [indexPath], with: .fade) 的调用。

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == .delete) {
let position = indexPath.row
labs.remove(at: position)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}

关于arrays - 想从tableview中删除一行,并从数组中删除对应的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56620605/

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