1

我有一些表格视图单元格,上面有一些数据,并且单元格上有一个十字按钮(在右上角),单击该按钮应该删除单元格。这就是我试图删除的方式......

extension sellTableViewController: imageDelegate {
    func delete(cell: sellTableViewCell) {
        if let indexPath = tableview?.indexPath(for: cell) {
            //1.Delete photo from datasource
            arrProduct?.remove(at: indexPath.row)
            print(self.appDelegate.commonArrForselectedItems)

            tableview.deleteRows(at: [indexPath], with: .fade)

        }
    }
}

但是当我点击十字按钮时,我收到一条错误消息说The number of sections contained in the table view after the update (1) must be equal to the number of sections contained in the table view before the update (2), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'

我的表格视图numberOfSections如下numberOfRowsInSection...

    func numberOfSections(in tableView: UITableView) -> Int {
        return (arrProduct?.count)!

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        let product = arrProduct![section]

        return product.images.count
    }

希望有人可以帮助...

4

2 回答 2

2

您正在从数组中删除项目 fromindexPath.row但您的数组包含部分而不是行

只需一行错误替换

        arrProduct?.remove(at: indexPath.row)

        arrProduct?.remove(at: indexPath.section)

希望对你有帮助

编辑

我认为您正在从数组中删除图像

arrProduct![indexPath.section].images.remove(at: indexPath.row)

于 2017-11-23T12:59:00.270 回答
0

您的代码混淆了部分和行。您是说部分的数量基于产品的数量 ( arrProduct?.count),而行数是基于产品中该部分的图像数量 ( arrProduct![section].images.count)。

但是在您的delete函数中,您删除了一个产品 ( arrProduct?.remove(at: indexPath.row)),它对应于一个部分,但随后您删除了表格上的一行。

确保在使用产品(部分)和图像(行)时清楚。

于 2017-11-23T13:12:38.217 回答