简体   繁体   English

我无法在Swift中删除最后一个表格视图单元格

[英]I can't delete my last table view cell in Swift

I am writing a shopping cart and everything seems to be working well but I can't delete the last row. 我在写购物车,一切似乎都运行良好,但是我无法删除最后一行。

I can delete but not the last row. 我可以删除,但不能删除最后一行。

override func viewDidLoad() {
    super.viewDidLoad()

    createLineItems()
}

override func numberOfSections(in tableView: UITableView) -> Int {
    if lineItems.count == 0 {
        return 0
    } else {
        return 1
    }
}

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

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if(editingStyle == .delete) {
        lineItems.remove(at: indexPath.row)

        tableView.beginUpdates()

        let indPath = IndexPath(item: indexPath.row, section: 0 )

        tableView.deleteRows(at: [indPath], with: .fade)
        tableView.endUpdates()

    }
}

func createLineItems() {
    lineItems = [LineItem]()

    lineItems.append(LineItem(frame:
        CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height),
                              index: 1)
        )


    lineItems.append(LineItem(frame:
        CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height),
                              index: 2)
    )

    lineItems.append(LineItem(frame:
        CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height),
                              index: 3)
    )

}

The error I get is printing out: 我得到的错误是打印出来:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. 由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“无效的更新:无效的节数。 The number of sections contained in the table view after the update (0) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).' 更新(0)之后表视图中包含的节数必须等于更新(1)前表视图中包含的节数,加上或减去插入或删除的节数(插入的0,0删除)。”

I have read: 我读过了:

https://www.hackingwithswift.com/example-code/uikit/how-to-remove-cells-from-a-uitableview https://www.hackingwithswift.com/example-code/uikit/how-to-remove-cells-from-a-uitableview

as well as other pages that I can't re-find. 以及其他我无法重新找到的页面。

I am aware I have to remove elements from my array first and then delete the row. 我知道我必须先从数组中删除元素,然后再删除该行。

I am aware that I am pretty sure I have to surround my row deletion with beginUpdates and endUpdates. 我知道我很确定我必须用beginUpdates和endUpdates包围行删除。

But I get that error every time. 但是我每次都会收到该错误。 I have tried 100 different variations. 我尝试了100种不同的变化。

The error says the row count has to be 1 less than the original number. 该错误表明行数必须比原始数少1。

I have no idea how it is not. 我不知道怎么回事。

The problems is here: 问题在这里:

override func numberOfSections(in tableView: UITableView) -> Int {
    if lineItems.count == 0 {
        return 0
    } else {
        return 1
    }
}

It should be: 它应该是:

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

If you delete the last row your code is claiming there are now no sections. 如果您删除最后一行,则代码将声明现在没有任何节。 But you are not deleting any sections, only rows. 但是您没有删除任何节,仅删除行。 This is confusing the table view and resulting in the error. 这会混淆表视图并导致错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM