'NSInternalInconsistencyException'试图将相同索引路径的多个单元格出列,这是不允许的

fis*_*erM 2 uitableview ios swift swift4

我的应用程序崩溃了

由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'尝试将同一索引路径的多个单元格出列,这是不允许的.如果您确实需要出现比表视图请求更多的单元格,请使用-dequeueReusableCellWithIdentifier:方法(不带索引路径).

当我尝试重新加载我的单个单元格时tableView,这是我按下以重新加载单元格的按钮

 @IBAction func reloadCell(_ sender: UIButton) {

        let index = IndexPath(row: sender.tag, section: 0)
        self.tableView.reloadRows(at: [index], with: .right)

    }
Run Code Online (Sandbox Code Playgroud)

这些是我的cellForRowDidSelectRow

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

            let cell = tableView.dequeueReusableCell(withIdentifier: resueIdentifier, for: indexPath) as! MyTableViewCell
            cell.delegate = self
            cell.myCellNumber.text = "\(indexPath.row + 1)"
            cell.refButton.tag = indexPath.row
            cell.refButton.addTarget(self, action: #selector(CourseClass2.reloadCell(_:)), for: .touchUpInside)
            let place = sortedArray[indexPath.row]
            cell.update(place: place)
            cell.selectionStyle = .none

             if indexPath.row == places.count - 1 {
                loadPlaces(false)
            }
            print("CellForRow Call")
            return (cell)


        }


   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


           tableView.deselectRow(at: indexPath, animated: true)

            UIView.animate(withDuration: 0.2, animations: {
            let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCell", for: indexPath) as! MyTableViewCell
        })
             performSegue(withIdentifier: "goToLast" , sender: indexPath)

        }
Run Code Online (Sandbox Code Playgroud)

添加断点我看到应用程序在DidSelectRow中的此行崩溃.

let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCell", for: indexPath) as! MyTableViewCell
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

Dha*_*esh 7

你的didSelectRowAt方法应该是:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


    tableView.deselectRow(at: indexPath, animated: true)
    self.performSegue(withIdentifier: "goToLast", sender: indexPath)

}
Run Code Online (Sandbox Code Playgroud)

如果你想在你的didSelectRowAt方法中识别你的细胞.你可以用:

if let cell = tableView.cellForRow(at: indexPath) as? MyTableViewCell {

}
Run Code Online (Sandbox Code Playgroud)

在你的didSelectRowAt方法.