gpt4 book ai didi

ios - 如何在 Swift 3 的 editingStyle 中识别 .delete 以删除 tableview 中的单元格

转载 作者:可可西里 更新时间:2023-11-01 00:50:30 24 4
gpt4 key购买 nike

这是连接到我的 TableView 的所有功能。为什么我不能删除行?

我也在 viewDidLoad

override func viewDidLoad() {
super.viewDidLoad()
self.myTableView.dataSource = self
self.myTableView.delegate = self
self.myTableView.reloadData()
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arr.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "incTV")

cell.textLabel?.text = "X"

cell.detailTextLabel?.text = "Y"

return cell
}


func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {

myTableView.deleteRows(at: [indexPath], with: .fade)
arr.remove(at: (indexPath as NSIndexPath).row)
self.myTableView.reloadData()
}
}

override func viewWillAppear(_ animated: Bool) {
myTableView.reloadData()
}

最佳答案

首先确保你的表有一个委托(delegate)集,并且你的委托(delegate)方法在委托(delegate)类/ Controller 中。

其次,尝试使用 canEditRowAtIndexPath 方法并返回 true。

关于ios - 如何在 Swift 3 的 editingStyle 中识别 .delete 以删除 tableview 中的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39936570/

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