【问题标题】:Invalid update: invalid number of rows while using Realm无效更新:使用 Realm 时行数无效
【发布时间】:2016-11-30 16:21:28
【问题描述】:

我不断收到以下错误,但是当我打印 getEquippedGear().count 的值时,我得到“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))
}

【问题讨论】:

  • 能否分享您的numberOfSectionsInTableViewnumberOfRowsInSectionUser.getEquippedGear()User.sellGear() 代码?
  • @kishikawakatsumi 我更新了帖子

标签: swift xcode uitableview realm


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 2014-10-28
    • 1970-01-01
    相关资源
    最近更新 更多