无效更新:使用Realm

时间:2016-07-26 20:54:06

标签: swift xcode uitableview realm

我一直收到下面的错误,但是当我打印getEquippedGear()的值时,我得到" 1",这是正确的。当它在sellGear完成处理程序中再次打印时,我得到" 0"。但是它会抛出此错误。我做错了什么?

错误

  

由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:'无效更新:第0部分中的行数无效。更新后现有部分中包含的行数(1)必须等于更新前的该部分中包含的行数(1),加上或减去从该部分插入或删除的行数(0插入,1删除)和加或减移入的行数或超出该部分(0移入,0移出)。'

代码

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
    }
}

getEquippedGear

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

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

1 个答案:

答案 0 :(得分:0)

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