Swift 在tableView中消除可重用单元崩溃

Swift 在tableView中消除可重用单元崩溃,swift,uitableview,Swift,Uitableview,在表视图中添加新单元格时遇到一些问题 奇怪的是,一旦函数正常工作,我就运行了,如果我再次运行,它就会因为这个错误而崩溃 *由于未捕获的异常“NSRangeException”而终止应用程序,原因:'*-[\uu\NSSingleObjectArrayI objectAtIndex:]:索引1超出边界[0..0]' 这是我的代码: override func viewWillAppear(_ animated: Bool) { if prodottoDaAggiungere != nil

在表视图中添加新单元格时遇到一些问题

奇怪的是,一旦函数正常工作,我就运行了,如果我再次运行,它就会因为这个错误而崩溃

*由于未捕获的异常“NSRangeException”而终止应用程序,原因:'*-[\uu\NSSingleObjectArrayI objectAtIndex:]:索引1超出边界[0..0]'

这是我的代码:

override func viewWillAppear(_ animated: Bool) {
    if prodottoDaAggiungere != nil {
        prodotti.append(prodottoDaAggiungere!)
        let indexPath = IndexPath(row: prodotti.count-1, section: 1)
        tableView.insertRows(at: [indexPath], with: .fade) // ?
        prodottoDaAggiungere = nil
    }
}

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

    if indexPath.row < prodotti.count && indexPath.section == 1 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ProdottoTableViewCell", for: indexPath) as! ProdottoTableViewCell // Crash :|
        let indexRow = indexPath.row // Fix
        cell.title.text = "\(prodotti[indexRow].prodotto.nome!) - \(prodotti[indexRow].prodotto.marca!)"
        cell.subtitle.text = "\(prodotti[indexRow].prodotto.formati[prodotti[indexRow].formato].dimensione) \(prodotti[indexRow].prodotto.unitàMisura!) - \(prodotti[indexRow].prodotto.formati[prodotti[indexRow].formato].prezzo) €"
        cell.number.text = Int(cell.stepper.value).description // 1
        cell.stepper.addTarget(self, action: #selector(stepperChanged), for: .valueChanged)
        return cell
    }

    return super.tableView(tableView, cellForRowAt: indexPath)
}

真的谢谢
AP

问题在于这一部分:

let indexPath = IndexPath(row: prodotti.count, section: 1)
如果
prodotti.count
为2,则当计数为2时,您试图滚动到索引为2的行。您需要切换它,以便将计数-1,如下所示:

let indexPath = IndexPath(row: prodotti.count - 1, section: 1)

此外,您需要使用添加的新项更新数组
prodotti
。由于该部分中的行数为
prodotti
+2,因此它在前两次工作,但由于您实际上没有增加
prodotti
的大小,因此tableview不知道现在应该有更多行。

此部分存在问题:

let indexPath = IndexPath(row: prodotti.count, section: 1)
如果
prodotti.count
为2,则当计数为2时,您试图滚动到索引为2的行。您需要切换它,以便将计数-1,如下所示:

let indexPath = IndexPath(row: prodotti.count - 1, section: 1)

此外,您需要使用添加的新项更新数组
prodotti
。由于该部分中的行数是
prodotti
+2,因此它在前两次工作,但由于您实际上没有增加
prodotti
的大小,因此tableview不知道现在应该有更多行。

数组的范围是从索引
0
N-1
,being
N=yourArray.count
。 所以你的上限应该是
yourray.count-1
。就你而言:

让indepath=indepath(行:prodotti.count-1,节:0)

请注意,这些部分也以
0
开头


换句话说:元素的数量不同于它们的实际索引,即它们的当前位置减去1。

数组的范围从索引
0
N-1
,即
N=yourArray.count
。 所以你的上限应该是
yourray.count-1
。就你而言:

让indepath=indepath(行:prodotti.count-1,节:0)

请注意,这些部分也以
0
开头


换句话说:元素的数量不同于它们的实际索引,即它们的当前位置减去1。

我为您提供了解决方案:

非常感谢:

我为您提供了解决方案:

非常感谢:

您可以共享prodotti数组初始化吗?请尝试将indexPath行设置为
prodotti.count-1
@Mac3n再次崩溃,但出现相同错误,但现在我可以使用该函数两次,第三次崩溃。您可以发布numberOfRowsInSection函数吗?永不,切勿在
cellForRowAt
之外调用
dequeueReusableCell
。它完全没有意义–在方法结束时创建并丢弃单元格–以及
beginUpdates/endUpdates
。您可以共享prodotti数组初始化吗?尝试将indexPath行设置为
prodotti.count-1
@Mac3n它再次崩溃,并出现相同的错误,但现在我可以使用该函数2次,第三个崩溃。你能发布numberOfRowsInSection函数吗?永远,永远不要调用
dequeueReusableCell
外部的
cellForRowAt
。这是完全没有意义的–单元格是在方法结束时创建并丢弃的–以及
beginUpdates/endUpdates
。嗨,非常感谢你的回答,我试图通过简化来修改问题,但添加了重要数据,也许我可以让你更好地理解。这就是为什么不是第0节,而是第1节。嗨,非常感谢您的回答,我试图通过简化问题来修改问题,但添加了重要数据,也许我可以让您更好地理解。这就是为什么不是第0节,而是第1节。嗨,非常感谢您的回答,我试图通过简化问题来修改问题,但添加了重要数据,也许我可以让您更好地理解。嗨,非常感谢你的回答,我试图通过简化问题来修改它,但添加了重要的数据,也许我可以让你更好地理解。