gpt4 book ai didi

swift - 无效更新: invalid number of rows while using Realm

转载 作者:行者123 更新时间:2023-11-30 13:12:24 24 4
gpt4 key购买 nike

我不断收到下面的错误,但是当我打印 getEquippedGear().count 的值时,我得到“1”,这是正确的。当它在 sellGear 完成处理程序期间再次打印时,我得到“0”。但是它会抛出此错误。我做错了什么?

错误

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 (1) must be equal to the number of rows contained in that section before the update (1), 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).'

代码

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

let actionSell = UITableViewRowAction(style: .Destructive, title: "Sell", handler: {_,_ in
if let cell = tableView.cellForRowAtIndexPath(indexPath) as? GearTableViewCell {
if let gear = cell.gear {
print("DEBUG: " + String(User.getEquippedGear()?.count))
User.sellGear(self, gear: gear, completionHandler: {
print("DEBUG: " + String(User.getEquippedGear()?.count))
tableView.beginUpdates()
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
tableView.endUpdates()
});
}
}
});
return [actionSell]
}

出售装备

static func sellGear(view: UIViewController, gear: Gear, completionHandler: (() -> Void)?) {
let alert = UIAlertController.init(title: "Are you sure you want to sell this item?", message: "", preferredStyle: .Alert)

alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { UIAlertAction in
view.presentedViewController?.dismissViewControllerAnimated(true, completion: nil)

completionHandler?()
}))

alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { UIAlertAction in
removeGear(gear)

User.addGold(500)

view.presentedViewController?.dismissViewControllerAnimated(true, completion: nil)

completionHandler?()
}))

view.presentViewController(alert, animated: true, completion: nil)
}

numberOfSectionsInTableView 和 numberOfRowsInSection

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
guard let data = User.getEquippedGear() else {
return 1
}
if data.count > 0 {
print("numberOfRowsInSection - Section: " + String(0) + " Gear Count: " + String(data.count))
return data.count
} else {
return 1
}
case 1:
guard let data = User.getUnequippedGear() else {
return 1
}
if data.count > 0 {
print("numberOfRowsInSection - Section: " + String(1) + " Gear Count: " + String(data.count))
return data.count
} else {
return 1
}
default:
return 1
}
}

获取装备装备

static func getEquippedGear() -> Results<Gear>? {
guard let gear = getGear() else {
return nil
} //Gets List<Gear>? from Realm

return gear.filter("isEquipped == %@", NSNumber(bool: true))
}

最佳答案

tableView(_:, numberOfRowsInSection:) 永远不会返回零。因此,即使 print("DEBUG: "+ String(User.getEquippedGear()?.count)) 显示零,tableView(_:, numberOfRowsInSection:) 也会返回 1。当您删除一行时, TableView 期望行数为零,但 tableView(_:, numberOfRowsInSection:) 返回 1。这就是发生异常的原因。

关于swift - 无效更新: invalid number of rows while using Realm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38599702/

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