gpt4 book ai didi

ios - Swift 3.0 无法从 UITableView 中删除一行

转载 作者:可可西里 更新时间:2023-11-01 01:29:30 25 4
gpt4 key购买 nike

我正在使用 Xcode 8 和 swift 3。

当我尝试从 UITableView 中删除一行时遇到断言失败。

Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3599.5/UITableView.m:1610

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (25) must be equal to the number of rows contained in that section before the update (25), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

代码:

// create a cell for each table view row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

// create a new cell if needed or reuse an old one
let cell:UserCell = self.tableView.dequeueReusableCell(withIdentifier: "tblCell") as! UserCell!

cell.titleLabel.text = self.animals[(indexPath as NSIndexPath).row]

cell.btnDelete.addTarget(self, action:#selector(ViewController.buttonClicked(sender:)), for: UIControlEvents.touchUpInside);

return cell
}

func buttonClicked(sender:UIButton) {
if let superview = sender.superview {
if let cell = superview.superview as? UserCell {
if let indexPath = self.tableView.indexPath(for: cell){
print("row = ",indexPath.row)
self.tableView.beginUpdates()
self.tableView.deleteRows(at: [indexPath], with: .left)
self.tableView.endUpdates()
self.animals.remove(at:indexPath.row)
}
}
}

}

我刚开始学习swift所以请帮助我。

最佳答案

尝试在 self.tableView.endUpdates() 之前和 self.tableView.beginUpdates 之后移动 self.animals.remove(at:indexPath.row) () 我认为这可能会解决您的问题。

关于ios - Swift 3.0 无法从 UITableView 中删除一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39594036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com