如何识别.delete在editingStyle斯威夫特3到删除的tableview细胞

问题描述 投票:2回答:1

这一切我FUNC是连接到我的表视图。为什么我不能删除行?

我也做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()
}
ios swift uitableview swift3
1个回答
4
投票

首先确保表中有一个代表组,并且您的委托方法是在委托类/控制器。

其次,尽量使用canEditRowAtIndexPath方法并返回true。

© www.soinside.com 2019 - 2024. All rights reserved.