簡體   English   中英

當我從TableView上的按鈕操作中刪除單元格時,應用程序崩潰了-iOS Swift

[英]When i remove cell from TableView on button action the app is crashed - iOS Swift

我試圖從TableViewController刪除一個單元格。 每次我滑動並按Delete鍵,都會發生崩潰。

這是崩潰日志:

由於未捕獲的異常“ NSInternalInconsistencyException”而終止應用程序,原因:“無效更新:第0節中的行數無效。更新(10)之后現有節中包含的行數必須等於該行中包含的行數更新前的部分(10),加上或減去從該部分插入或刪除的行數(已插入0,已刪除1),加上或減去該部分移入或移出的行數(移入0,移入0)出)。”

這是我的UITableView函數。

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let myCell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath) as! PredefinedServicesCell

    //Cell configuration.
    myCell.selectionStyle = .None
    myCell.containerView.layer.borderColor = UIColor(hex: 0x3399CC).CGColor
    myCell.containerView.layer.borderWidth = 1
    myCell.containerView.layer.cornerRadius = 10
    myCell.containerView.clipsToBounds = true
    myCell.servicePrice.text = "\(indexPath.row)"
    myCell.serviceCurrency.text = "KWD"
    myCell.serviceTitle.text = "Building iOS Application"
    myCell.serviceDescription.text = "This is a service description This is a service description This is a service description This is a service description This is a service description This is a service description This is a service description This is a service description This is a service description"
    return myCell
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    if editingStyle == .Delete {
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    }

}

原因可能是什么?

請動態添加您的numberOfRowsInSection 它不是靜態的。

使用返回值為10的數組插入,並以編輯方式刪除時刪除objec。

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

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    if editingStyle == .Delete {
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

// remove object from array here

    }

}

錯誤原因:根據您的情況

第一個表單元格計數為10。當您刪除單元格時,您的單元格被刪除,但計數為10。

記錄已刪除單元格的數量,並從數組中刪除相同數量(用於填充表視圖)。

您不能為numberOfRowsInSection聲明靜態值 ,然后通過刪除帶有deleteRowsAtIndexPaths的行來更改它( deleteRowsAtIndexPathsnumberOfRowsInSection保持10並且行變為9,所以您遇到這種問題,請使這些更改具有動態值

var totalRows: Int = 10

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.totalRows
}

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    if editingStyle == .Delete {
        if self.totalRows > 0 { // This limit your deletion to avoid a crash
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
            self.totalRows -= 1
        }
    }
}

通常,最好處理一個數據源數組以保持信息顯示在每個單元格中,因此請考慮使用數組來執行此操作,以便在刪除單元格時可以使用它的count屬性並刪除其元素。

從表格視圖中刪除行后,必須還必須更新numberOfRowsInSection值。 這就是為什么您會崩潰。

let rows=10
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return rows
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let myCell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath) as! PredefinedServicesCell

        //Cell configuration.
        myCell.selectionStyle = .None
        myCell.containerView.layer.borderColor = UIColor(hex: 0x3399CC).CGColor
        myCell.containerView.layer.borderWidth = 1
        myCell.containerView.layer.cornerRadius = 10
        myCell.containerView.clipsToBounds = true
        myCell.servicePrice.text = "\(indexPath.row)"
        myCell.serviceCurrency.text = "KWD"
        myCell.serviceTitle.text = "Building iOS Application"
        myCell.serviceDescription.text = "This is a service description This is a service description This is a service description This is a service description This is a service description This is a service description This is a service description This is a service description This is a service description"
        return myCell
    }
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

        if editingStyle == .Delete {
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
            rows-=1
        }

    }

在崩潰日志之后,您應該在編輯后更新行數

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}

刪除單元格后,不能將單元格數返回10。 它應該少於刪除前的單元格數。

您必須更新數據源以匹配表視圖的行/節的數量,並使用beginUpdate和endUpdate更新表視圖的顯示。 像這樣:

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    if editingStyle == .Delete {
        tableView.beginUpdate() //optional
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        //TODO ... add your code to update data source here

        tableView.endUpdate() //optional
    }

}

您的numberOfRowsInSection應該是動態的,而不是靜態的。 因此,使用yourdataName.remove(at:indexPath.row)刪除動態數組中的數據

例;

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

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
                if editingStyle == UITableViewCellEditingStyle.delete {
                    yourdataName.remove(at:indexPath.row)
                    tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
                }
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM