繁体   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